Finished re-reading of "classes vs. interfaces"
1.1 --- a/samples/growingparameters/nbproject/project.xml Sat Jun 14 09:53:37 2008 +0200
1.2 +++ b/samples/growingparameters/nbproject/project.xml Sat Jun 14 09:53:38 2008 +0200
1.3 @@ -60,8 +60,12 @@
1.4 <location>src-api1.0</location>
1.5 </source-folder>
1.6 <source-folder style="packages">
1.7 + <label>src-api2.0</label>
1.8 + <location>src-api2.0</location>
1.9 + </source-folder>
1.10 + <source-folder style="packages">
1.11 <label>impl</label>
1.12 - <location>impl</location>
1.13 + <location>src-impl</location>
1.14 </source-folder>
1.15 <source-file>
1.16 <location>build.xml</location>
2.1 --- a/samples/growingparameters/src-api1.0/api/classes/Support.java Sat Jun 14 09:53:37 2008 +0200
2.2 +++ b/samples/growingparameters/src-api1.0/api/classes/Support.java Sat Jun 14 09:53:38 2008 +0200
2.3 @@ -1,17 +1,8 @@
2.4 -/*
2.5 - * To change this template, choose Tools | Templates
2.6 - * and open the template in the editor.
2.7 - */
2.8 -
2.9 package api.classes;
2.10
2.11 import java.util.Iterator;
2.12 import java.util.List;
2.13
2.14 -/**
2.15 - *
2.16 - * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
2.17 - */
2.18 public class Support {
2.19 private Support() {
2.20 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/growingparameters/src-api1.0/api/request/response/Compute.java Sat Jun 14 09:53:38 2008 +0200
3.3 @@ -0,0 +1,31 @@
3.4 +package api.request.response;
3.5 +
3.6 +import java.util.List;
3.7 +
3.8 +public interface Compute {
3.9 + public void computeData(Request request, Response response);
3.10 +
3.11 + /** Right now useless, but may be handy in future */
3.12 + public final class Request {
3.13 + /** allow access just from friend code */
3.14 + Request() {
3.15 + }
3.16 + }
3.17 +
3.18 + /** Allows clients to provide the computed strings */
3.19 + public final class Response {
3.20 + private final List<String> result;
3.21 + /** Allow access only to friend code */
3.22 + Response(List<String> result) {
3.23 + this.result = result;
3.24 + }
3.25 +
3.26 + public void add(String s) {
3.27 + this.result.add(s);
3.28 + }
3.29 +
3.30 + public void addAll(List<String> all) {
3.31 + this.result.addAll(all);
3.32 + }
3.33 + }
3.34 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/samples/growingparameters/src-api1.0/api/request/response/Support.java Sat Jun 14 09:53:38 2008 +0200
4.3 @@ -0,0 +1,27 @@
4.4 +package api.request.response;
4.5 +
4.6 +import java.util.ArrayList;
4.7 +import java.util.Iterator;
4.8 +import java.util.List;
4.9 +
4.10 +public class Support {
4.11 + private Support() {
4.12 + }
4.13 +
4.14 + public static int searchByName(String name, Compute provider) {
4.15 + List<String> result = new ArrayList<String>();
4.16 + Compute.Request request = new Compute.Request();
4.17 + Compute.Response response = new Compute.Response(result);
4.18 +
4.19 + provider.computeData(request, response);
4.20 +
4.21 + Iterator<String> it = result.iterator();
4.22 + for (int i = 0; it.hasNext(); i++) {
4.23 + if (name.equals(it.next())) {
4.24 + return i;
4.25 + }
4.26 + }
4.27 + return -1;
4.28 + }
4.29 +
4.30 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/samples/growingparameters/src-api2.0/api/request/response/Compute.java Sat Jun 14 09:53:38 2008 +0200
5.3 @@ -0,0 +1,40 @@
5.4 +package api.request.response;
5.5 +
5.6 +import java.util.List;
5.7 +import java.util.Map;
5.8 +
5.9 +// BEGIN: grow.request.response
5.10 +public interface Compute {
5.11 + public void computeData(Request request, Response response);
5.12 +
5.13 + public final class Request {
5.14 + // only getters public, rest hidden only for friend code
5.15 + Request() {
5.16 + }
5.17 + }
5.18 +
5.19 + public final class Response {
5.20 + // only setters public, rest available only for friend code
5.21 + private final Map<String,String> result;
5.22 + /** Allow access only to friend code */
5.23 + Response(Map<String,String> result) {
5.24 + this.result = result;
5.25 + }
5.26 +
5.27 + public void add(String s) {
5.28 + this.result.put(s, s);
5.29 + }
5.30 +
5.31 + public void addAll(List<String> all) {
5.32 + for (String s : all) {
5.33 + add(s);
5.34 + }
5.35 + }
5.36 +
5.37 + /** @since 2.0 */
5.38 + public void add(String s, String description) {
5.39 + this.result.put(s, description);
5.40 + }
5.41 + }
5.42 +}
5.43 +// END: grow.request.response
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/samples/growingparameters/src-api2.0/api/request/response/Support.java Sat Jun 14 09:53:38 2008 +0200
6.3 @@ -0,0 +1,44 @@
6.4 +package api.request.response;
6.5 +
6.6 +import java.util.ArrayList;
6.7 +import java.util.Iterator;
6.8 +import java.util.LinkedHashMap;
6.9 +import java.util.List;
6.10 +import java.util.Map;
6.11 +
6.12 +public class Support {
6.13 + private Support() {
6.14 + }
6.15 +
6.16 + public static int searchByName(String name, Compute provider) {
6.17 + Map<String,String> result = new LinkedHashMap<String,String>();
6.18 + Compute.Request request = new Compute.Request();
6.19 + Compute.Response response = new Compute.Response(result);
6.20 +
6.21 + provider.computeData(request, response);
6.22 +
6.23 + Iterator<String> it = result.keySet().iterator();
6.24 + for (int i = 0; it.hasNext(); i++) {
6.25 + if (name.equals(it.next())) {
6.26 + return i;
6.27 + }
6.28 + }
6.29 + return -1;
6.30 + }
6.31 +
6.32 + public static int searchByDescription(String name, Compute provider) {
6.33 + Map<String,String> result = new LinkedHashMap<String,String>();
6.34 + Compute.Request request = new Compute.Request();
6.35 + Compute.Response response = new Compute.Response(result);
6.36 +
6.37 + provider.computeData(request, response);
6.38 +
6.39 + Iterator<String> it = result.values().iterator();
6.40 + for (int i = 0; it.hasNext(); i++) {
6.41 + if (name.equals(it.next())) {
6.42 + return i;
6.43 + }
6.44 + }
6.45 + return -1;
6.46 + }
6.47 +}
7.1 --- a/samples/growingparameters/src-impl/impl/Impl.java Sat Jun 14 09:53:37 2008 +0200
7.2 +++ b/samples/growingparameters/src-impl/impl/Impl.java Sat Jun 14 09:53:38 2008 +0200
7.3 @@ -1,26 +1,35 @@
7.4 package impl;
7.5
7.6 -import api.classes.Compute;
7.7 -import api.classes.Support;
7.8 import java.util.Collections;
7.9 import java.util.List;
7.10 import java.util.Map;
7.11
7.12 public class Impl {
7.13 public static void main(String[] args) {
7.14 - class ProviderWrittenAgainstVersion1 extends Compute {
7.15 + System.err.println("showUsageOfAbstractClassAPI:");
7.16 + showUsageOfAbstractClassAPI();
7.17 + System.err.println("showUsageOfRequestResponseAPI:");
7.18 + showUsageOfRequestResponseAPI();
7.19 + }
7.20 +
7.21 + private static void assertEquals(String msg, int expected, int real) {
7.22 + System.err.println(" " + msg + " expected: " + expected + " was: " + real);
7.23 + assert expected == real;
7.24 + }
7.25 +
7.26 + private static void showUsageOfAbstractClassAPI() {
7.27 + class ProviderWrittenAgainstVersion1 extends api.classes.Compute {
7.28 @Override
7.29 public List<String> getData() {
7.30 return Collections.singletonList("Hello");
7.31 }
7.32 }
7.33 - int index1 = Support.searchByDescription("Hello", new ProviderWrittenAgainstVersion1());
7.34 - assert index1 == 0;
7.35 - int index2 = Support.searchByDescription("Unknown", new ProviderWrittenAgainstVersion1());
7.36 - assert index2 == -1;
7.37 -
7.38 + int index1 = api.classes.Support.searchByDescription("Hello", new ProviderWrittenAgainstVersion1());
7.39 + assertEquals("Hello was found", 0, index1);
7.40 + int index2 = api.classes.Support.searchByDescription("Unknown", new ProviderWrittenAgainstVersion1());
7.41 + assertEquals("Unknown was not found", -1, index2);
7.42
7.43 - class ProviderWrittenAgainstVersion2 extends Compute {
7.44 + class ProviderWrittenAgainstVersion2 extends api.classes.Compute {
7.45 @Override
7.46 public List<String> getData() {
7.47 return Collections.singletonList("Hello");
7.48 @@ -32,7 +41,31 @@
7.49 }
7.50 }
7.51
7.52 - int index3 = Support.searchByDescription("Says hello", new ProviderWrittenAgainstVersion2());
7.53 + int index3 = api.classes.Support.searchByDescription("Says hello", new ProviderWrittenAgainstVersion2());
7.54 + assertEquals("Found by description", 0, index3);
7.55 + }
7.56 +
7.57 +
7.58 +
7.59 + private static void showUsageOfRequestResponseAPI() {
7.60 + class ProviderWrittenAgainstVersion1 implements api.request.response.Compute {
7.61 + public void computeData(Request request, Response response) {
7.62 + response.add("Hello");
7.63 + }
7.64 + }
7.65 + int index1 = api.request.response.Support.searchByDescription("Hello", new ProviderWrittenAgainstVersion1());
7.66 + assertEquals("Hello found", 0, index1);
7.67 + int index2 = api.request.response.Support.searchByDescription("Unknown", new ProviderWrittenAgainstVersion1());
7.68 + assertEquals("Unknown not found", -1, index2);
7.69 +
7.70 + class ProviderWrittenAgainstVersion2 implements api.request.response.Compute {
7.71 + public void computeData(Request request, Response response) {
7.72 + response.add("Hello", "Says hello");
7.73 + }
7.74 + }
7.75 +
7.76 + int index3 = api.request.response.Support.searchByDescription("Says hello", new ProviderWrittenAgainstVersion2());
7.77 + assertEquals("Description found", 0, index3);
7.78 assert index3 == 0;
7.79 }
7.80 }