JaroslavTulach: /* Summary */ - 2016-06-13 07:55:54

Summary

←Older revision Revision as of 07:55, 13 June 2016
Line 82: Line 82:
== Summary ==
== Summary ==
-
By giving the [[builder]] a generic error type we allow fine grain control over possible errors states that may accumulate when calling various [[builder]] configuration methods. The whole solution keeps the runtime behavior of the [[builder]] pattern - after applying [[erasure]] of generic types, the whole type information (visible to compiler and [[API] user) disappears.
+
By giving the [[builder]] a generic error type we allow fine grain control over possible errors states that may accumulate when calling various [[builder]] configuration methods. The whole solution keeps the runtime behavior of the [[builder]] pattern - after applying [[erasure]] of generic types, the whole type information (visible to compiler and [[API]] user) disappears.

JaroslavTulach: /* Summary */ - 2016-06-13 07:55:44

Summary

←Older revision Revision as of 07:55, 13 June 2016
Line 82: Line 82:
== Summary ==
== Summary ==
-
By giving the [[builder]] a generic error type we allow fine grain control over possible errors states that may accumulate when calling various [[builder]] configuration methods. The whole solution keeps the runtime behavior of the [[builder]] pattern - after applying [[erasure]] of generic types, the whole type information (visible to compiler) disappears.
+
By giving the [[builder]] a generic error type we allow fine grain control over possible errors states that may accumulate when calling various [[builder]] configuration methods. The whole solution keeps the runtime behavior of the [[builder]] pattern - after applying [[erasure]] of generic types, the whole type information (visible to compiler and [[API] user) disappears.

JaroslavTulach: /* Changing the State */ - 2016-06-13 07:54:34

Changing the State

←Older revision Revision as of 07:54, 13 June 2016
Line 72: Line 72:
</source>
</source>
-
Obviously, when one specifies the content directly, there is no I/O when the ''build()'' method is called and there should be no need to require catching an {{JDK|java/io|IOException}}. With the above change one can start with a builder based on ''newFromFile'', and still switch to mode that doesn't throw an {{JDK|java/io|IOException}} at all:
+
Obviously, when one specifies the content directly, there is no I/O when the ''build()'' method is called and there should be no need to require catching of an {{JDK|java/io|IOException}}. With the above change one can start with a builder based on ''newFromFile'', and still switch to mode that doesn't throw an {{JDK|java/io|IOException}} at all:
<source lang="java">
<source lang="java">

JaroslavTulach: /* Builder for Source */ - 2016-06-13 07:51:54

Builder for Source

←Older revision Revision as of 07:51, 13 June 2016
Line 3: Line 3:
== Builder for Source ==
== Builder for Source ==
-
These days [[I]] am trying to use the [[builder]] pattern also for construction of {{truffle|com/oracle/truffle/api/source|Source}}. Rather than having various (and overloaded) methods name '''fromFileName''' with various number and order of parameters, etc. we'd like to have:
+
These days [[I]] am trying to use the [[builder]] pattern also for construction of {{truffle|com/oracle/truffle/api/source|Source}}. Rather than having various (and overloaded) methods named '''fromFileName''' with various number and order of parameters, etc. we'd like to have:
<source lang="java">
<source lang="java">

JaroslavTulach: /* Changing the State */ - 2016-06-10 14:16:12

Changing the State

←Older revision Revision as of 14:16, 10 June 2016
Line 72: Line 72:
</source>
</source>
-
Now one can start with a builder based on ''newFromFile'', and still switch to mode that doesn't throw an {{JDK|java/io|IOException}} at all:
+
Obviously, when one specifies the content directly, there is no I/O when the ''build()'' method is called and there should be no need to require catching an {{JDK|java/io|IOException}}. With the above change one can start with a builder based on ''newFromFile'', and still switch to mode that doesn't throw an {{JDK|java/io|IOException}} at all:
<source lang="java">
<source lang="java">
Source source = Source.newFromFile(file).
Source source = Source.newFromFile(file).
-
mimetype("text/javascript").
 
-
name("FancyName.js").
 
content("var x = 42\n").
content("var x = 42\n").
build(); // no IOException at all
build(); // no IOException at all

JaroslavTulach: /* Throw only for I/O */ - 2016-06-10 14:13:42

Throw only for I/O

←Older revision Revision as of 14:13, 10 June 2016
Line 38: Line 38:
=== Throw only for I/O ===
=== Throw only for I/O ===
-
The option that [[I]] came up with is to parametrize the builder with the exception the ''build()'' method is going to throw:
+
The option that [[I]] came up with is to parametrize the [[builder]] with the exception the ''build()'' method is going to throw:
<source lang="java">
<source lang="java">

JaroslavTulach: /* Throw only for I/O */ - 2016-06-10 14:13:28

Throw only for I/O

←Older revision Revision as of 14:13, 10 June 2016
Line 38: Line 38:
=== Throw only for I/O ===
=== Throw only for I/O ===
-
The option that [[I]] come up with is to parametrize the builder with the exception the ''build()'' method is going to throw:
+
The option that [[I]] came up with is to parametrize the builder with the exception the ''build()'' method is going to throw:
<source lang="java">
<source lang="java">

JaroslavTulach: /* Throw on I/O Error */ - 2016-06-10 14:12:35

Throw on I/O Error

←Older revision Revision as of 14:12, 10 June 2016
Line 25: Line 25:
</source>
</source>
-
However there is more. The [[builder]] may also construct a {{truffle|com/oracle/truffle/api/source|Source} object from already provided text. The usage looks like:
+
However there is more. The [[builder]] may also construct a {{truffle|com/oracle/truffle/api/source|Source}} object from already provided text. The usage looks like:
<source lang="java">
<source lang="java">

JaroslavTulach: /* Builder for Source */ - 2016-06-10 14:11:40

Builder for Source

←Older revision Revision as of 14:11, 10 June 2016
Line 3: Line 3:
== Builder for Source ==
== Builder for Source ==
-
These days [[I]] am trying to use the [[builder]] pattern also for construction of {{truffle|com/oracle/truffle/api/source|Source}}. Rather than having various (and overloaded) methods with '''fromFileName''', etc. we'd like to have:
+
These days [[I]] am trying to use the [[builder]] pattern also for construction of {{truffle|com/oracle/truffle/api/source|Source}}. Rather than having various (and overloaded) methods name '''fromFileName''' with various number and order of parameters, etc. we'd like to have:
<source lang="java">
<source lang="java">

JaroslavTulach: /* Changing the State */ - 2016-06-10 14:09:12

Changing the State

←Older revision Revision as of 14:09, 10 June 2016
Line 72: Line 72:
</source>
</source>
-
Now one can start with a builder based on ''newFromFile'', and still switch to mode that doesn't throw an exception at all.
+
Now one can start with a builder based on ''newFromFile'', and still switch to mode that doesn't throw an {{JDK|java/io|IOException}} at all:
 +
 
 +
<source lang="java">
 +
Source source = Source.newFromFile(file).
 +
mimetype("text/javascript").
 +
name("FancyName.js").
 +
content("var x = 42\n").
 +
build(); // no IOException at all
 +
</source>
 +
 
 +
== Summary ==
 +
 
 +
By giving the [[builder]] a generic error type we allow fine grain control over possible errors states that may accumulate when calling various [[builder]] configuration methods. The whole solution keeps the runtime behavior of the [[builder]] pattern - after applying [[erasure]] of generic types, the whole type information (visible to compiler) disappears.
 +
 
 +
 
 +
[[Category:APIDesignPatterns]]
 +
[[Category:APIDesignPatterns:Creational]]
 +
[[Category:APIDesignPatterns:Exceptions]]