1.1 --- a/samples/composition/src-api1.0/api/Arithmetica.java Sat Jun 14 10:04:51 2008 +0200
1.2 +++ b/samples/composition/src-api1.0/api/Arithmetica.java Sat Jun 14 10:04:53 2008 +0200
1.3 @@ -24,6 +24,10 @@
1.4
1.5 public int sumRange(int from, int to) {
1.6 int len = to - from;
1.7 + if (len < 0) {
1.8 + len = -len;
1.9 + from = to;
1.10 + }
1.11 int[] array = new int[len + 1];
1.12 for (int i = 0; i <= len; i++) {
1.13 array[i] = from + i;
2.1 --- a/samples/composition/src-api2.0-compat/api/Arithmetica.java Sat Jun 14 10:04:51 2008 +0200
2.2 +++ b/samples/composition/src-api2.0-compat/api/Arithmetica.java Sat Jun 14 10:04:53 2008 +0200
2.3 @@ -43,6 +43,10 @@
2.4
2.5 private int sumRange1(int from, int to) {
2.6 int len = to - from;
2.7 + if (len < 0) {
2.8 + len = -len;
2.9 + from = to;
2.10 + }
2.11 int[] array = new int[len + 1];
2.12 for (int i = 0; i <= len; i++) {
2.13 array[i] = from + i;
2.14 @@ -51,7 +55,7 @@
2.15 }
2.16
2.17 private int sumRange2(int from, int to) {
2.18 - return (from + to) * (to - from + 1) / 2;
2.19 + return (from + to) * (Math.abs(to - from) + 1) / 2;
2.20 }
2.21 }
2.22 // END: design.composition.arith2.0.compat
3.1 --- a/samples/composition/src-api2.0-enum/api/Arithmetica.java Sat Jun 14 10:04:51 2008 +0200
3.2 +++ b/samples/composition/src-api2.0-enum/api/Arithmetica.java Sat Jun 14 10:04:53 2008 +0200
3.3 @@ -49,6 +49,10 @@
3.4
3.5 private int sumRange1(int from, int to) {
3.6 int len = to - from;
3.7 + if (len < 0) {
3.8 + len = -len;
3.9 + from = to;
3.10 + }
3.11 int[] array = new int[len + 1];
3.12 for (int i = 0; i <= len; i++) {
3.13 array[i] = from + i;
3.14 @@ -57,6 +61,6 @@
3.15 }
3.16
3.17 private int sumRange2(int from, int to) {
3.18 - return (from + to) * (to - from + 1) / 2;
3.19 + return (from + to) * (Math.abs(to - from) + 1) / 2;
3.20 }
3.21 }
4.1 --- a/samples/composition/src-api2.0-property/api/Arithmetica.java Sat Jun 14 10:04:51 2008 +0200
4.2 +++ b/samples/composition/src-api2.0-property/api/Arithmetica.java Sat Jun 14 10:04:53 2008 +0200
4.3 @@ -36,6 +36,10 @@
4.4
4.5 private int sumRange1(int from, int to) {
4.6 int len = to - from;
4.7 + if (len < 0) {
4.8 + len = -len;
4.9 + from = to;
4.10 + }
4.11 int[] array = new int[len + 1];
4.12 for (int i = 0; i <= len; i++) {
4.13 array[i] = from + i;
4.14 @@ -44,7 +48,7 @@
4.15 }
4.16
4.17 private int sumRange2(int from, int to) {
4.18 - return (from + to) * (to - from + 1) / 2;
4.19 + return (from + to) * (Math.abs(to - from) + 1) / 2;
4.20 }
4.21 }
4.22 // END: design.composition.arith2.0.property
5.1 --- a/samples/composition/src-api2.0-runtime/api/Arithmetica.java Sat Jun 14 10:04:51 2008 +0200
5.2 +++ b/samples/composition/src-api2.0-runtime/api/Arithmetica.java Sat Jun 14 10:04:53 2008 +0200
5.3 @@ -35,6 +35,10 @@
5.4
5.5 private int sumRange1(int from, int to) {
5.6 int len = to - from;
5.7 + if (len < 0) {
5.8 + len = -len;
5.9 + from = to;
5.10 + }
5.11 int[] array = new int[len + 1];
5.12 for (int i = 0; i <= len; i++) {
5.13 array[i] = from + i;
5.14 @@ -43,7 +47,7 @@
5.15 }
5.16
5.17 private int sumRange2(int from, int to) {
5.18 - return (from + to) * (to - from + 1) / 2;
5.19 + return (from + to) * (Math.abs(to - from) + 1) / 2;
5.20 }
5.21
5.22 private static boolean calledByV2AwareLoader() {
6.1 --- a/samples/composition/src-api2.0/api/Arithmetica.java Sat Jun 14 10:04:51 2008 +0200
6.2 +++ b/samples/composition/src-api2.0/api/Arithmetica.java Sat Jun 14 10:04:53 2008 +0200
6.3 @@ -24,7 +24,7 @@
6.4
6.5 // BEGIN: design.composition.arith2.0
6.6 public int sumRange(int from, int to) {
6.7 - return (from + to) * (to - from + 1) / 2;
6.8 + return (from + to) * (Math.abs(to - from) + 1) / 2;
6.9 }
6.10 // END: design.composition.arith2.0
6.11 }
7.1 --- a/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java Sat Jun 14 10:04:51 2008 +0200
7.2 +++ b/samples/composition/src-test/api/ArithmeticaCompatibilityTest.java Sat Jun 14 10:04:53 2008 +0200
7.3 @@ -66,15 +66,15 @@
7.4 compare(now, old, seed);
7.5
7.6 assertEquals(
7.7 - "Verify amount of sumRange is the same",
7.8 + "Verify amount calls to of sumRange is the same",
7.9 now.countSumRange, old.countSumRange
7.10 );
7.11 assertEquals(
7.12 - "Verify amount of sumAll is the same",
7.13 + "Verify amount calls to of sumAll is the same",
7.14 now.countSumAll, old.countSumAll
7.15 );
7.16 assertEquals(
7.17 - "Verify amount of sumTwo is the same",
7.18 + "Verify amount calls to of sumTwo is the same",
7.19 now.countSumTwo, old.countSumTwo
7.20 );
7.21 } catch (AssertionFailedError ex) {
7.22 @@ -99,15 +99,15 @@
7.23 compare(now, old, 1208120436947L);
7.24
7.25 assertEquals(
7.26 - "Verify amount of sumRange is the same",
7.27 + "Verify amount of calls to sumRange is the same",
7.28 now.countSumRange, old.countSumRange
7.29 );
7.30 assertEquals(
7.31 - "Verify amount of sumAll is the same",
7.32 + "Verify amount of calls to sumAll is the same",
7.33 now.countSumAll, old.countSumAll
7.34 );
7.35 assertEquals(
7.36 - "Verify amount of sumTwo is the same",
7.37 + "Verify amount of calls to sumTwo is the same",
7.38 now.countSumTwo, old.countSumTwo
7.39 );
7.40 }
7.41 @@ -119,15 +119,15 @@
7.42 compare(now, old, 1208120628821L);
7.43
7.44 assertEquals(
7.45 - "Verify amount of sumRange is the same",
7.46 + "Verify amount of calls to sumRange is the same",
7.47 now.countSumRange, old.countSumRange
7.48 );
7.49 assertEquals(
7.50 - "Verify amount of sumAll is the same",
7.51 + "Verify amount of calls to sumAll is the same",
7.52 now.countSumAll, old.countSumAll
7.53 );
7.54 assertEquals(
7.55 - "Verify amount of sumTwo is the same",
7.56 + "Verify amount of calls to sumTwo is the same",
7.57 now.countSumTwo, old.countSumTwo
7.58 );
7.59 }
8.1 --- a/samples/composition/src-test/api/ArithmeticaTest.java Sat Jun 14 10:04:51 2008 +0200
8.2 +++ b/samples/composition/src-test/api/ArithmeticaTest.java Sat Jun 14 10:04:53 2008 +0200
8.3 @@ -35,8 +35,10 @@
8.4
8.5 public void testSumRange() {
8.6 Arithmetica instance = new Arithmetica();
8.7 - assertEquals("+", 6, instance.sumRange(1, 3));
8.8 - assertEquals("10", 55, instance.sumRange(1, 10));
8.9 + assertEquals("1+2+3=6", 6, instance.sumRange(1, 3));
8.10 + assertEquals("sum(1,10)=55", 55, instance.sumRange(1, 10));
8.11 + assertEquals("sum(1,1)=1", 1, instance.sumRange(1, 1));
8.12 + assertEquals("sum(10,1)=55", 55, instance.sumRange(10, 1));
8.13 }
8.14 //END: design.composition.arith.test
8.15
9.1 --- a/samples/composition/src-test/api/FactorialTest.java Sat Jun 14 10:04:51 2008 +0200
9.2 +++ b/samples/composition/src-test/api/FactorialTest.java Sat Jun 14 10:04:53 2008 +0200
9.3 @@ -38,22 +38,4 @@
9.4 public void testFactorial5() {
9.5 assertEquals(120, Factorial.factorial(5));
9.6 }
9.7 -
9.8 - /** Class showing inventive, non-expected use of
9.9 - * Arithmetica methods to do multiplication instead of
9.10 - * addition.
9.11 - */
9.12 - //BEGIN: design.composition.arith.factorial
9.13 - public static final class Factorial extends Arithmetica {
9.14 - public static int factorial(int n) {
9.15 - return new Factorial().sumRange(1, n);
9.16 - }
9.17 - @Override
9.18 - public int sumTwo(int one, int second) {
9.19 - return one * second;
9.20 - }
9.21 - }
9.22 - //END: design.composition.arith.factorial
9.23 -
9.24 -
9.25 }
10.1 --- a/samples/consistency/src-api1.0/api/Lookups.java Sat Jun 14 10:04:51 2008 +0200
10.2 +++ b/samples/consistency/src-api1.0/api/Lookups.java Sat Jun 14 10:04:53 2008 +0200
10.3 @@ -1,6 +1,9 @@
10.4 package api;
10.5
10.6 +import java.util.ArrayList;
10.7 import java.util.Collection;
10.8 +import java.util.HashSet;
10.9 +import java.util.List;
10.10 import java.util.Set;
10.11
10.12 /** Factory to create various types of lookup instances.
10.13 @@ -12,7 +15,39 @@
10.14 private Lookups() {
10.15 }
10.16
10.17 - public static Lookup fixed(Object... instances) {
10.18 - return null;
10.19 + public static Lookup fixed(final Object... instances) {
10.20 + return new Lookup() {
10.21 + @Override
10.22 + public <T> T lookup(Class<T> clazz) {
10.23 + for (Object obj : instances) {
10.24 + if (clazz.isInstance(obj)) {
10.25 + return clazz.cast(obj);
10.26 + }
10.27 + }
10.28 + return null;
10.29 + }
10.30 +
10.31 + @Override
10.32 + public <T> Collection<? extends T> lookupAll(Class<T> clazz) {
10.33 + List<T> result = new ArrayList<T>();
10.34 + for (Object obj : instances) {
10.35 + if (clazz.isInstance(obj)) {
10.36 + result.add(clazz.cast(obj));
10.37 + }
10.38 + }
10.39 + return result;
10.40 + }
10.41 +
10.42 + @Override
10.43 + public <T> Set<Class<? extends T>> lookupAllClasses(Class<T> clazz) {
10.44 + Set<Class<? extends T>> result = new HashSet<Class<? extends T>>();
10.45 + for (Object obj : instances) {
10.46 + if (clazz.isInstance(obj)) {
10.47 + result.add(obj.getClass().asSubclass(clazz));
10.48 + }
10.49 + }
10.50 + return result;
10.51 + }
10.52 + };
10.53 }
10.54 }
11.1 --- a/samples/consistency/src-api2.0/api/Lookup.java Sat Jun 14 10:04:51 2008 +0200
11.2 +++ b/samples/consistency/src-api2.0/api/Lookup.java Sat Jun 14 10:04:53 2008 +0200
11.3 @@ -1,6 +1,11 @@
11.4 package api;
11.5
11.6 +import java.util.ArrayList;
11.7 import java.util.Collection;
11.8 +import java.util.Collections;
11.9 +import java.util.HashSet;
11.10 +import java.util.Iterator;
11.11 +import java.util.List;
11.12 import java.util.Set;
11.13
11.14 /** Simplified version of NetBeans
11.15 @@ -13,26 +18,49 @@
11.16 * @version 2.0
11.17 */
11.18 // BEGIN: design.consistency.2.0
11.19 -public final class Lookup {
11.20 +public abstract class Lookup {
11.21 + /** only for classes in the same package */
11.22 Lookup() {
11.23 }
11.24
11.25 // BEGIN: design.consistency.lookup.2.0
11.26 public <T> T lookup(Class<T> clazz) {
11.27 - return null;
11.28 + Iterator<T> it = doLookup(clazz);
11.29 + return it.hasNext() ? it.next() : null;
11.30 }
11.31 // END: design.consistency.lookup.2.0
11.32
11.33 // BEGIN: design.consistency.lookupAll.2.0
11.34 public <T> Collection<? extends T> lookupAll(Class<T> clazz) {
11.35 - return null;
11.36 + Iterator<T> it = doLookup(clazz);
11.37 + if (!it.hasNext()) {
11.38 + return Collections.emptyList();
11.39 + } else {
11.40 + List<T> result = new ArrayList<T>();
11.41 + while (it.hasNext()) {
11.42 + result.add(it.next());
11.43 + }
11.44 + return result;
11.45 + }
11.46 }
11.47 // END: design.consistency.lookupAll.2.0
11.48
11.49 // BEGIN: design.consistency.lookupAllClasses.2.0
11.50 public <T> Set<Class<? extends T>> lookupAllClasses(Class<T> clazz) {
11.51 - return null;
11.52 + Iterator<T> it = doLookup(clazz);
11.53 + if (!it.hasNext()) {
11.54 + return Collections.emptySet();
11.55 + } else {
11.56 + Set<Class<? extends T>> result =
11.57 + new HashSet<Class<? extends T>>();
11.58 + while (it.hasNext()) {
11.59 + result.add(it.next().getClass().asSubclass(clazz));
11.60 + }
11.61 + return result;
11.62 + }
11.63 }
11.64 // END: design.consistency.lookupAllClasses.2.0
11.65 +// FINISH: design.consistency.2.0
11.66 +
11.67 + abstract <T> Iterator<T> doLookup(Class<T> clazz);
11.68 }
11.69 -// END: design.consistency.2.0
11.70 \ No newline at end of file
12.1 --- a/samples/consistency/src-api2.0/api/Lookups.java Sat Jun 14 10:04:51 2008 +0200
12.2 +++ b/samples/consistency/src-api2.0/api/Lookups.java Sat Jun 14 10:04:53 2008 +0200
12.3 @@ -1,6 +1,8 @@
12.4 package api;
12.5
12.6 +import java.util.ArrayList;
12.7 import java.util.Collection;
12.8 +import java.util.Iterator;
12.9 import java.util.Set;
12.10
12.11 /** Factory to create various types of lookup instances.
12.12 @@ -12,12 +14,30 @@
12.13 private Lookups() {
12.14 }
12.15
12.16 - public static Lookup fixed(Object... instances) {
12.17 - return null;
12.18 + public static Lookup fixed(final Object... instances) {
12.19 + return new Lookup() {
12.20 + @Override
12.21 + <T> Iterator<T> doLookup(Class<T> clazz) {
12.22 + ArrayList<T> result = new ArrayList<T>();
12.23 + for (Object obj : instances) {
12.24 + if (clazz.isInstance(obj)) {
12.25 + result.add(clazz.cast(obj));
12.26 + }
12.27 + }
12.28 + return result.iterator();
12.29 + }
12.30 + };
12.31 }
12.32
12.33 - public static Lookup dynamic(Dynamic provider) {
12.34 - return null;
12.35 + public static Lookup dynamic(final Dynamic provider) {
12.36 + return new Lookup() {
12.37 + @Override
12.38 + <T> Iterator<T> doLookup(Class<T> clazz) {
12.39 + ArrayList<T> result = new ArrayList<T>();
12.40 + provider.computeInstances(clazz, result);
12.41 + return result.iterator();
12.42 + }
12.43 + };
12.44 }
12.45
12.46 public interface Dynamic {
13.1 --- a/samples/deadlock/src/org/apidesign/deadlock/logs/Parallel.java Sat Jun 14 10:04:51 2008 +0200
13.2 +++ b/samples/deadlock/src/org/apidesign/deadlock/logs/Parallel.java Sat Jun 14 10:04:53 2008 +0200
13.3 @@ -5,7 +5,7 @@
13.4 import java.util.logging.Logger;
13.5
13.6 // BEGIN: test.parallel
13.7 -class Parael implements Runnable {
13.8 +class Parallel implements Runnable {
13.9 public void run() {
13.10 Random r = new Random();
13.11 for (int i = 0; i < 10; i++) {
13.12 @@ -16,8 +16,8 @@
13.13 }
13.14 }
13.15 public static void main(String[] args) throws InterruptedException {
13.16 - Thread t1 = new Thread(new Parael(), "1st");
13.17 - Thread t2 = new Thread(new Parael(), "2nd");
13.18 + Thread t1 = new Thread(new Parallel(), "1st");
13.19 + Thread t2 = new Thread(new Parallel(), "2nd");
13.20 t1.start(); t2.start();
13.21 t1.join(); t2.join();
13.22 }
14.1 --- a/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelControlFlowTest.java Sat Jun 14 10:04:51 2008 +0200
14.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelControlFlowTest.java Sat Jun 14 10:04:53 2008 +0200
14.3 @@ -47,7 +47,7 @@
14.4 "THREAD: 2nd MSG: cnt: 9",
14.5 500
14.6 );
14.7 - Parael.main(null);
14.8 + Parallel.main(null);
14.9 fail("Ok, just print the logged output");
14.10 }
14.11 // END: test.parallel.test.controlflow
14.12 @@ -61,7 +61,7 @@
14.13 "THREAD: 1st MSG: cnt: 6",
14.14 5000
14.15 );
14.16 - Parael.main(null);
14.17 + Parallel.main(null);
14.18 fail("Ok, just print the logged output");
14.19 }
14.20 // END: test.parallel.test.fivetwo
15.1 --- a/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelSortedTest.java Sat Jun 14 10:04:51 2008 +0200
15.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelSortedTest.java Sat Jun 14 10:04:53 2008 +0200
15.3 @@ -25,7 +25,7 @@
15.4
15.5 public void testMain() throws Exception {
15.6 Logger.global.addHandler(new BlockingHandler());
15.7 - Parael.main(null);
15.8 + Parallel.main(null);
15.9 fail("Ok, just print the logged output");
15.10 }
15.11
16.1 --- a/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelTest.java Sat Jun 14 10:04:51 2008 +0200
16.2 +++ b/samples/deadlock/test/org/apidesign/deadlock/logs/ParallelTest.java Sat Jun 14 10:04:53 2008 +0200
16.3 @@ -21,7 +21,7 @@
16.4 }
16.5
16.6 public void testMain() throws Exception {
16.7 - Parael.main(null);
16.8 + Parallel.main(null);
16.9 fail("Ok, just print logged messages");
16.10 }
16.11 }
17.1 --- a/samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java Sat Jun 14 10:04:51 2008 +0200
17.2 +++ b/samples/delegatingwriter/src/org/apidesign/delegatingwriter/AltBufferedWriter.java Sat Jun 14 10:04:53 2008 +0200
17.3 @@ -68,7 +68,7 @@
17.4 // BEGIN: writer.delegateout
17.5 // efficient, yet dangerous delegation skipping methods unknown to
17.6 // subclasses that used version 1.4
17.7 - if (csq != null && csq.length() < 1024) {
17.8 + if (shouldBufferAsTheSequenceIsNotTooBig(csq)) {
17.9 write(csq.toString());
17.10 } else {
17.11 flush();
17.12 @@ -103,7 +103,7 @@
17.13 throw new IOException(ex);
17.14 }
17.15
17.16 - if (isOverriden || (csq != null && csq.length() < 1024)) {
17.17 + if (isOverriden || shouldBufferAsTheSequenceIsNotTooBig(csq)) {
17.18 write(csq.toString());
17.19 } else {
17.20 flush();
17.21 @@ -112,6 +112,28 @@
17.22 return this;
17.23 // END: writer.conditionally
17.24 }
17.25 +
17.26 + /** At the end the purpose of BufferedWriter is to buffer writes, this
17.27 + * method is here to decide when it is OK to prefer buffering and when
17.28 + * it is better to delegate directly into the underlaying stream.
17.29 + *
17.30 + * @param csq the seqence to evaluate
17.31 + * @return true if buffering from super class should be used
17.32 + */
17.33 + private static boolean shouldBufferAsTheSequenceIsNotTooBig(CharSequence csq) {
17.34 + if (csq == null) {
17.35 + return false;
17.36 + }
17.37 + // as buffers are usually bigger than 1024, it makes sense to
17.38 + // pay the penalty of converting the sequence to string, but buffering
17.39 + // the write
17.40 + if (csq.length() < 1024) {
17.41 + return true;
17.42 + } else {
17.43 + // otherwise, just directly write down the char sequence
17.44 + return false;
17.45 + }
17.46 + }
17.47
17.48 public enum Behaviour {
17.49 THROW_EXCEPTION,
18.1 --- a/samples/delegatingwriter/test/org/apidesign/delegatingwriter/CountingWriter.java Sat Jun 14 10:04:51 2008 +0200
18.2 +++ b/samples/delegatingwriter/test/org/apidesign/delegatingwriter/CountingWriter.java Sat Jun 14 10:04:53 2008 +0200
18.3 @@ -6,8 +6,6 @@
18.4
18.5 // BEGIN: writer.CountingWriter
18.6 /** Writer that counts the number of written in characters.
18.7 - *
18.8 - * @author Jaroslav Tulach
18.9 */
18.10 public class CountingWriter extends Writer {
18.11 private int counter;
19.1 --- a/samples/delegatingwriter/test/org/apidesign/delegatingwriter/CryptoWriter.java Sat Jun 14 10:04:51 2008 +0200
19.2 +++ b/samples/delegatingwriter/test/org/apidesign/delegatingwriter/CryptoWriter.java Sat Jun 14 10:04:53 2008 +0200
19.3 @@ -34,26 +34,26 @@
19.4 public void write(char[] buf, int off, int len) throws IOException {
19.5 char[] arr = new char[len];
19.6 for (int i = 0; i < len; i++) {
19.7 - arr[i] = convertChar(buf[off + i]);
19.8 + arr[i] = encryptChar(buf[off + i]);
19.9 }
19.10 super.write(arr, 0, len);
19.11 }
19.12
19.13 @Override
19.14 public void write(int c) throws IOException {
19.15 - super.write(convertChar(c));
19.16 + super.write(encryptChar(c));
19.17 }
19.18
19.19 @Override
19.20 public void write(String str, int off, int len) throws IOException {
19.21 StringBuffer sb = new StringBuffer();
19.22 for (int i = 0; i < len; i++) {
19.23 - sb.append(convertChar(str.charAt(off + i)));
19.24 + sb.append(encryptChar(str.charAt(off + i)));
19.25 }
19.26 super.write(sb.toString(), 0, len);
19.27 }
19.28
19.29 - private char convertChar(int c) {
19.30 + private char encryptChar(int c) {
19.31 if (c == 'Z') {
19.32 return 'A';
19.33 }
20.1 --- a/samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java Sat Jun 14 10:04:51 2008 +0200
20.2 +++ b/samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java Sat Jun 14 10:04:53 2008 +0200
20.3 @@ -28,7 +28,7 @@
20.4 }
20.5
20.6 public void write(CharSequence seq) throws IOException {
20.7 - if (seq.length() < 1024) {
20.8 + if (shouldBufferAsTheSequenceIsNotTooBig(seq)) {
20.9 sb.append(seq);
20.10 } else {
20.11 flush();
20.12 @@ -36,4 +36,26 @@
20.13 }
20.14 }
20.15
20.16 + /** At the end the purpose of BufferedWriter is to buffer writes, this
20.17 + * method is here to decide when it is OK to prefer buffering and when
20.18 + * it is better to delegate directly into the underlaying stream.
20.19 + *
20.20 + * @param csq the seqence to evaluate
20.21 + * @return true if buffering from super class should be used
20.22 + */
20.23 + private static boolean shouldBufferAsTheSequenceIsNotTooBig(CharSequence csq) {
20.24 + if (csq == null) {
20.25 + return false;
20.26 + }
20.27 + // as buffers are usually bigger than 1024, it makes sense to
20.28 + // pay the penalty of converting the sequence to string, but buffering
20.29 + // the write
20.30 + if (csq.length() < 1024) {
20.31 + return true;
20.32 + } else {
20.33 + // otherwise, just directly write down the char sequence
20.34 + return false;
20.35 + }
20.36 + }
20.37 +
20.38 }
21.1 --- a/samples/delegatingwriterfinal/src-api2.0/api/Writer.java Sat Jun 14 10:04:51 2008 +0200
21.2 +++ b/samples/delegatingwriterfinal/src-api2.0/api/Writer.java Sat Jun 14 10:04:53 2008 +0200
21.3 @@ -97,8 +97,8 @@
21.4 return new Writer(impl, null);
21.5 }
21.6
21.7 - public static Writer create(ImplSeq impl) {
21.8 - return new Writer(null, impl);
21.9 + public static Writer create(ImplSeq seq) {
21.10 + return new Writer(null, seq);
21.11 }
21.12
21.13 public static Writer create(final java.io.Writer w) {
22.1 --- a/samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/CountingWriter.java Sat Jun 14 10:04:51 2008 +0200
22.2 +++ b/samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/CountingWriter.java Sat Jun 14 10:04:53 2008 +0200
22.3 @@ -6,8 +6,6 @@
22.4 import java.util.concurrent.atomic.AtomicInteger;
22.5
22.6 /** Writer that counts the number of written in characters.
22.7 - *
22.8 - * @author Jaroslav Tulach
22.9 */
22.10 public class CountingWriter implements Writer.ImplSeq {
22.11 private final AtomicInteger counter;
23.1 --- a/samples/growingparameters/src-api1.0/api/request/response/Compute.java Sat Jun 14 10:04:51 2008 +0200
23.2 +++ b/samples/growingparameters/src-api1.0/api/request/response/Compute.java Sat Jun 14 10:04:53 2008 +0200
23.3 @@ -21,11 +21,11 @@
23.4 }
23.5
23.6 public void add(String s) {
23.7 - this.result.add(s);
23.8 + result.add(s);
23.9 }
23.10
23.11 public void addAll(List<String> all) {
23.12 - this.result.addAll(all);
23.13 + result.addAll(all);
23.14 }
23.15 }
23.16 }
24.1 --- a/samples/growingparameters/src-api2.0/api/request/response/Compute.java Sat Jun 14 10:04:51 2008 +0200
24.2 +++ b/samples/growingparameters/src-api2.0/api/request/response/Compute.java Sat Jun 14 10:04:53 2008 +0200
24.3 @@ -22,7 +22,7 @@
24.4 }
24.5
24.6 public void add(String s) {
24.7 - this.result.put(s, s);
24.8 + result.put(s, s);
24.9 }
24.10
24.11 public void addAll(List<String> all) {
24.12 @@ -33,7 +33,7 @@
24.13
24.14 /** @since 2.0 */
24.15 public void add(String s, String description) {
24.16 - this.result.put(s, description);
24.17 + result.put(s, description);
24.18 }
24.19 }
24.20 }
25.1 --- a/samples/instanceof/src/api/InstanceProvider.java Sat Jun 14 10:04:51 2008 +0200
25.2 +++ b/samples/instanceof/src/api/InstanceProvider.java Sat Jun 14 10:04:53 2008 +0200
25.3 @@ -3,7 +3,7 @@
25.4
25.5 // BEGIN: instanceof.InstanceProvider
25.6 public interface InstanceProvider {
25.7 - public Class<?> instanceClass();
25.8 - public Object instanceCreate();
25.9 + public Class<?> instanceClass() throws Exception;
25.10 + public Object instanceCreate() throws Exception;
25.11 }
25.12 // END: instanceof.InstanceProvider
26.1 --- a/samples/instanceofclass/src-api1.0/api/InstanceProvider.java Sat Jun 14 10:04:51 2008 +0200
26.2 +++ b/samples/instanceofclass/src-api1.0/api/InstanceProvider.java Sat Jun 14 10:04:53 2008 +0200
26.3 @@ -2,18 +2,21 @@
26.4 package api;
26.5
26.6 // BEGIN: instanceof.class.InstanceProvider1
26.7 +
26.8 +import java.util.concurrent.Callable;
26.9 +
26.10 public final class InstanceProvider {
26.11 - private final Object instance;
26.12 + private final Callable<Object> instance;
26.13
26.14 - public InstanceProvider(Object instance) {
26.15 + public InstanceProvider(Callable<Object> instance) {
26.16 this.instance = instance;
26.17 }
26.18
26.19 - public Class<?> instanceClass() {
26.20 - return instance.getClass();
26.21 + public Class<?> instanceClass() throws Exception {
26.22 + return instance.call().getClass();
26.23 }
26.24 - public Object instanceCreate() {
26.25 - return instance;
26.26 + public Object instanceCreate() throws Exception {
26.27 + return instance.call();
26.28 }
26.29 }
26.30 // END: instanceof.class.InstanceProvider1
27.1 --- a/samples/instanceofclass/src-api2.0/api/InstanceProvider.java Sat Jun 14 10:04:51 2008 +0200
27.2 +++ b/samples/instanceofclass/src-api2.0/api/InstanceProvider.java Sat Jun 14 10:04:53 2008 +0200
27.3 @@ -2,24 +2,50 @@
27.4 package api;
27.5
27.6 // BEGIN: instanceof.class.InstanceProvider2
27.7 +
27.8 +import java.util.Arrays;
27.9 +import java.util.HashSet;
27.10 +import java.util.Set;
27.11 +import java.util.concurrent.Callable;
27.12 +
27.13 public final class InstanceProvider {
27.14 - private final Object instance;
27.15 + private final Callable<Object> instance;
27.16 + private final Set<String> types;
27.17
27.18 - public InstanceProvider(Object instance) {
27.19 + public InstanceProvider(Callable<Object> instance) {
27.20 this.instance = instance;
27.21 + this.types = null;
27.22 + }
27.23 + /** Specifies not only a factory for creating objects, but
27.24 + * also additional information about them.
27.25 + * @param instance the factory to create the object
27.26 + * @param type the class that the create object will be instance of
27.27 + * @since 2.0
27.28 + */
27.29 + public InstanceProvider(Callable<Object> instance, String... types) {
27.30 + this.instance = instance;
27.31 + this.types = new HashSet<String>();
27.32 + this.types.addAll(Arrays.asList(types));
27.33 }
27.34
27.35 - public Class<?> instanceClass() {
27.36 - return instance.getClass();
27.37 + public Class<?> instanceClass() throws Exception {
27.38 + return instance.call().getClass();
27.39 }
27.40 - public Object instanceCreate() {
27.41 - return instance;
27.42 + public Object instanceCreate() throws Exception {
27.43 + return instance.call();
27.44 }
27.45
27.46 - /** @since 2.0 */
27.47 - public boolean isInstanceOf(Class<?> c) {
27.48 - if (Helper.knownHowToDoItBetter()) {
27.49 - return Helper.computeTheResultOfIsInstanceOfInSomeBetterWay(c);
27.50 + /** Allows to find out if the InstanceProvider creates object of given
27.51 + * type. This check can be done without loading the actual object or
27.52 + * its implementation class into memory.
27.53 + *
27.54 + * @param c class to test
27.55 + * @return if the instances produced by this provider is instance of c
27.56 + * @since 2.0
27.57 + */
27.58 + public boolean isInstanceOf(Class<?> c) throws Exception {
27.59 + if (types != null) {
27.60 + return types.contains(c.getName());
27.61 } else {
27.62 // fallback
27.63 return c.isAssignableFrom(instanceClass());
27.64 @@ -29,12 +55,3 @@
27.65
27.66 }
27.67 // END: instanceof.class.InstanceProvider2
27.68 -
27.69 -class Helper {
27.70 - static boolean computeTheResultOfIsInstanceOfInSomeBetterWay(Class<?> c) {
27.71 - return false;
27.72 - }
27.73 - static boolean knownHowToDoItBetter() {
27.74 - return false;
27.75 - }
27.76 -}
27.77 \ No newline at end of file
28.1 --- a/samples/misuse/src/org/apidesign/misuse/Connection.java Sat Jun 14 10:04:51 2008 +0200
28.2 +++ b/samples/misuse/src/org/apidesign/misuse/Connection.java Sat Jun 14 10:04:53 2008 +0200
28.3 @@ -2,10 +2,10 @@
28.4
28.5 // BEGIN: misuse.Connection
28.6 public interface Connection {
28.7 - Savepoint setSavepoint();
28.8 + public Savepoint setSavepoint();
28.9
28.10 public interface Savepoint {
28.11 - void rollback();
28.12 + public void rollback();
28.13 // and other useful operations
28.14 }
28.15 }
29.1 --- a/samples/misuse/src/org/apidesign/misuse/projectconfig/ProjectConfigurationCorrect.java Sat Jun 14 10:04:51 2008 +0200
29.2 +++ b/samples/misuse/src/org/apidesign/misuse/projectconfig/ProjectConfigurationCorrect.java Sat Jun 14 10:04:53 2008 +0200
29.3 @@ -4,9 +4,9 @@
29.4 // BEGIN: misuse.prjconfig.correct
29.5 interface ProjectConfigurationProvider
29.6 <Configuration extends ProjectConfiguration> {
29.7 - Configuration[] getConfigurations();
29.8 - Configuration getActive();
29.9 - void setActive(Configuration c);
29.10 + public Configuration[] getConfigurations();
29.11 + public Configuration getActive();
29.12 + public void setActive(Configuration c);
29.13 }
29.14 interface ProjectConfiguration {
29.15 public String getDisplayName();
29.16 @@ -24,12 +24,13 @@
29.17 }
29.18 */
29.19
29.20 - static {
29.21 - // BEGIN: misuse.prjconfig.correct.access
29.22 + static
29.23 + // BEGIN: misuse.prjconfig.correct.access
29.24 + {
29.25 ProjectConfigurationProvider<?> provider = null; // obtain elsewhere;
29.26 resetToZero(provider);
29.27 - // END: misuse.prjconfig.correct.access
29.28 }
29.29 + // END: misuse.prjconfig.correct.access
29.30
29.31 // BEGIN: misuse.prjconfig.correct.openmethod
29.32 private static <C extends ProjectConfiguration> void resetToZero(
30.1 --- a/samples/misuse/src/org/apidesign/misuse/projectconfig/ProjectConfigurationOriginal.java Sat Jun 14 10:04:51 2008 +0200
30.2 +++ b/samples/misuse/src/org/apidesign/misuse/projectconfig/ProjectConfigurationOriginal.java Sat Jun 14 10:04:53 2008 +0200
30.3 @@ -3,9 +3,9 @@
30.4 public class ProjectConfigurationOriginal {
30.5 // BEGIN: misuse.prjconfig.orig
30.6 interface ProjectConfigurationProvider {
30.7 - ProjectConfiguration[] getConfigurations();
30.8 - ProjectConfiguration getActive();
30.9 - void setActive(ProjectConfiguration c);
30.10 + public ProjectConfiguration[] getConfigurations();
30.11 + public ProjectConfiguration getActive();
30.12 + public void setActive(ProjectConfiguration c);
30.13 }
30.14 interface ProjectConfiguration {
30.15 public String getDisplayName();
31.1 --- a/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrant.java Sat Jun 14 10:04:51 2008 +0200
31.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrant.java Sat Jun 14 10:04:53 2008 +0200
31.3 @@ -5,13 +5,13 @@
31.4
31.5 public class CriticalSectionReentrant<T extends Comparable<T>> implements CriticalSection<T> {
31.6 private T pilot;
31.7 - private AtomicInteger cnt = new AtomicInteger();
31.8
31.9 public void assignPilot(T pilot) {
31.10 this.pilot = pilot;
31.11 }
31.12
31.13 // BEGIN: reentrant.merge.int
31.14 + private AtomicInteger cnt = new AtomicInteger();
31.15 public int sumBigger(Collection<T> args) {
31.16 T pilotCopy = this.pilot;
31.17 int own = doCriticalSection(args, pilotCopy);
32.1 --- a/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantImmutable.java Sat Jun 14 10:04:51 2008 +0200
32.2 +++ b/samples/reentrant/src/org/apidesign/reentrant/CriticalSectionReentrantImmutable.java Sat Jun 14 10:04:53 2008 +0200
32.3 @@ -13,7 +13,10 @@
32.4 // BEGIN: reentrant.merge.immutable
32.5 public int sumBigger(Collection<T> args) {
32.6 for (;;) {
32.7 - ImmutableData<T> previous = this.data;
32.8 + ImmutableData<T> previous;
32.9 + synchronized (this) {
32.10 + previous = this.data;
32.11 + }
32.12 int[] ret = { 0 };
32.13 ImmutableData<T> now = doCriticalSection(args, previous, ret);
32.14