Compute with added method converted to separate project
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:53:37 +0200
changeset 7722c1953e372c
parent 76 ee4e8d38093e
child 78 af48bccb02cb
Compute with added method converted to separate project
samples/growingparameters/build.xml
samples/growingparameters/nbproject/project.xml
samples/growingparameters/src-api1.0/api/classes/Compute.java
samples/growingparameters/src-api1.0/api/classes/Support.java
samples/growingparameters/src-api2.0/api/classes/Compute.java
samples/growingparameters/src-api2.0/api/classes/Support.java
samples/growingparameters/src-impl/impl/Impl.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/growingparameters/build.xml	Sat Jun 14 09:53:37 2008 +0200
     1.3 @@ -0,0 +1,51 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project name="growingparameters" default="run" basedir=".">
     1.6 +    <target name="clean">
     1.7 +        <delete dir="build"/>
     1.8 +    </target>
     1.9 +    
    1.10 +    <target name="build">
    1.11 +        <antcall target="-build-one">
    1.12 +            <param name="version" value="api1.0"/>
    1.13 +        </antcall>
    1.14 +        <antcall target="-build-one">
    1.15 +            <param name="version" value="api2.0"/>
    1.16 +        </antcall>
    1.17 +        <antcall target="-build-one">
    1.18 +            <param name="version" value="impl"/>
    1.19 +            <param name="cp" location="build/api2.0/classes"/>
    1.20 +        </antcall>
    1.21 +    </target>
    1.22 +    
    1.23 +    <target name="run" depends="build">
    1.24 +        <echo level="info" message="Running the Implementation against Version 2.0 of the API. This should succeeds."/>
    1.25 +        <antcall target="-run-one">
    1.26 +            <param name="version" value="api2.0"/>
    1.27 +        </antcall>
    1.28 +    </target>
    1.29 +
    1.30 +    
    1.31 +    <!-- support methods -->
    1.32 +    
    1.33 +    <target name="-run-one">
    1.34 +        <fail message="You need to specify API version number" unless="version"/>
    1.35 +        <java classpath="build/${version}/classes:build/impl/classes" classname="impl.Impl"
    1.36 +            failonerror="true" 
    1.37 +        >
    1.38 +            <jvmarg value="-ea"/>
    1.39 +        </java>
    1.40 +    </target>
    1.41 +    
    1.42 +    <target name="-build-one">
    1.43 +        <fail message="You need to specify version number" unless="version"/>
    1.44 +        
    1.45 +        <mkdir dir="build/${version}/classes"/>
    1.46 +        <property name="cp" value=""/>
    1.47 +        <javac 
    1.48 +            srcdir="src-${version}" 
    1.49 +            destdir="build/${version}/classes" 
    1.50 +            source="1.5" target="1.5"
    1.51 +            classpath="${cp}"
    1.52 +        />
    1.53 +    </target>
    1.54 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/growingparameters/nbproject/project.xml	Sat Jun 14 09:53:37 2008 +0200
     2.3 @@ -0,0 +1,98 @@
     2.4 +<?xml version="1.0" encoding="UTF-8"?>
     2.5 +<project xmlns="http://www.netbeans.org/ns/project/1" xmlns:ns4="null">
     2.6 +    <type>org.netbeans.modules.ant.freeform</type>
     2.7 +    <configuration>
     2.8 +        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
     2.9 +            <name>growingparameters</name>
    2.10 +        </general-data>
    2.11 +        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
    2.12 +            <!-- Do not use Project Properties customizer when editing this file manually. -->
    2.13 +            <name>growingparameters</name>
    2.14 +            <properties/>
    2.15 +            <folders>
    2.16 +                <source-folder>
    2.17 +                    <label>growingparameters</label>
    2.18 +                    <location>.</location>
    2.19 +                    <encoding>UTF-8</encoding>
    2.20 +                </source-folder>
    2.21 +                <source-folder>
    2.22 +                    <label>src-api1.0</label>
    2.23 +                    <type>java</type>
    2.24 +                    <location>src-api1.0</location>
    2.25 +                    <encoding>UTF-8</encoding>
    2.26 +                </source-folder>
    2.27 +                <source-folder>
    2.28 +                    <label>impl</label>
    2.29 +                    <type>java</type>
    2.30 +                    <location>impl</location>
    2.31 +                    <encoding>UTF-8</encoding>
    2.32 +                </source-folder>
    2.33 +            </folders>
    2.34 +            <ide-actions>
    2.35 +                <action name="build">
    2.36 +                    <target>build</target>
    2.37 +                </action>
    2.38 +                <action name="clean">
    2.39 +                    <target>clean</target>
    2.40 +                </action>
    2.41 +                <action name="run">
    2.42 +                    <target>run</target>
    2.43 +                </action>
    2.44 +                <action name="rebuild">
    2.45 +                    <target>clean</target>
    2.46 +                    <target>build</target>
    2.47 +                </action>
    2.48 +            </ide-actions>
    2.49 +            <export>
    2.50 +                <type>folder</type>
    2.51 +                <location>build/api1.0/classes</location>
    2.52 +                <build-target>build</build-target>
    2.53 +            </export>
    2.54 +            <export>
    2.55 +                <type>folder</type>
    2.56 +                <location>build/api2.0/classes</location>
    2.57 +                <build-target>build</build-target>
    2.58 +            </export>
    2.59 +            <view>
    2.60 +                <items>
    2.61 +                    <source-folder style="packages">
    2.62 +                        <label>src-api1.0</label>
    2.63 +                        <location>src-api1.0</location>
    2.64 +                    </source-folder>
    2.65 +                    <source-folder style="packages">
    2.66 +                        <label>impl</label>
    2.67 +                        <location>impl</location>
    2.68 +                    </source-folder>
    2.69 +                    <source-file>
    2.70 +                        <location>build.xml</location>
    2.71 +                    </source-file>
    2.72 +                </items>
    2.73 +                <context-menu>
    2.74 +                    <ide-action name="build"/>
    2.75 +                    <ide-action name="rebuild"/>
    2.76 +                    <ide-action name="clean"/>
    2.77 +                    <ide-action name="run"/>
    2.78 +                    <ide-action name="debug"/>
    2.79 +                </context-menu>
    2.80 +            </view>
    2.81 +            <subprojects/>
    2.82 +        </general-data>
    2.83 +        <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
    2.84 +            <compilation-unit>
    2.85 +                <package-root>src-api1.0</package-root>
    2.86 +                <built-to>build/api1.0/classes</built-to>
    2.87 +                <source-level>1.5</source-level>
    2.88 +            </compilation-unit>
    2.89 +            <compilation-unit>
    2.90 +                <package-root>src-api2.0</package-root>
    2.91 +                <built-to>build/api2.0/classes</built-to>
    2.92 +                <source-level>1.5</source-level>
    2.93 +            </compilation-unit>
    2.94 +            <compilation-unit>
    2.95 +                <package-root>src-impl</package-root>
    2.96 +                <classpath mode="compile">src-api2.0</classpath>
    2.97 +                <source-level>1.5</source-level>
    2.98 +            </compilation-unit>
    2.99 +        </java-data>
   2.100 +    </configuration>
   2.101 +</project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/growingparameters/src-api1.0/api/classes/Compute.java	Sat Jun 14 09:53:37 2008 +0200
     3.3 @@ -0,0 +1,7 @@
     3.4 +package api.classes;
     3.5 +
     3.6 +import java.util.List;
     3.7 +
     3.8 +public abstract class Compute {
     3.9 +    public abstract List<String> getData();
    3.10 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/growingparameters/src-api1.0/api/classes/Support.java	Sat Jun 14 09:53:37 2008 +0200
     4.3 @@ -0,0 +1,29 @@
     4.4 +/*
     4.5 + * To change this template, choose Tools | Templates
     4.6 + * and open the template in the editor.
     4.7 + */
     4.8 +
     4.9 +package api.classes;
    4.10 +
    4.11 +import java.util.Iterator;
    4.12 +import java.util.List;
    4.13 +
    4.14 +/**
    4.15 + *
    4.16 + * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
    4.17 + */
    4.18 +public class Support {
    4.19 +    private Support() {
    4.20 +    }
    4.21 +    
    4.22 +    public static int searchByName(String name, Compute provider) {
    4.23 +        Iterator<String> it = provider.getData().iterator();
    4.24 +        for (int i = 0; it.hasNext(); i++) {
    4.25 +            if (name.equals(it.next())) {
    4.26 +                return i;
    4.27 +            }
    4.28 +        }
    4.29 +        return -1;
    4.30 +    }
    4.31 +    
    4.32 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/growingparameters/src-api2.0/api/classes/Compute.java	Sat Jun 14 09:53:37 2008 +0200
     5.3 @@ -0,0 +1,27 @@
     5.4 +package api.classes;
     5.5 +
     5.6 +import java.util.LinkedHashMap;
     5.7 +import java.util.List;
     5.8 +import java.util.Map;
     5.9 +
    5.10 +// BEGIN: grow.compute
    5.11 +public abstract class Compute {
    5.12 +    /**
    5.13 +     * @return list of strings to work with 
    5.14 +     * @since 1.0 */
    5.15 +    public abstract List<String> getData();
    5.16 +    /** Computes the strings to work with together with their associated descriptions.
    5.17 +     * Shall be overriden in subclasses. By default delegates to {@link #getData}
    5.18 +     * and uses the provided strings as both, the string and its description.
    5.19 +     * 
    5.20 +     * @return name to description pairs to work with 
    5.21 +     * @since 2.0 */
    5.22 +    public Map<String,String> getDataAndDescription() {
    5.23 +        LinkedHashMap<String,String> ret = new LinkedHashMap<String, String>();
    5.24 +        for (String s : getData()) {
    5.25 +            ret.put(s, s);
    5.26 +        }
    5.27 +        return ret;
    5.28 +    }
    5.29 +}
    5.30 +// END: grow.compute
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/growingparameters/src-api2.0/api/classes/Support.java	Sat Jun 14 09:53:37 2008 +0200
     6.3 @@ -0,0 +1,31 @@
     6.4 +package api.classes;
     6.5 +
     6.6 +import java.util.Iterator;
     6.7 +import java.util.Map.Entry;
     6.8 +
     6.9 +public class Support {
    6.10 +    private Support() {
    6.11 +    }
    6.12 +    
    6.13 +    public static int searchByName(String name, Compute provider) {
    6.14 +        Iterator<String> it = provider.getData().iterator();
    6.15 +        for (int i = 0; it.hasNext(); i++) {
    6.16 +            if (name.equals(it.next())) {
    6.17 +                return i;
    6.18 +            }
    6.19 +        }
    6.20 +        return -1;
    6.21 +    }
    6.22 +
    6.23 +    /** @since 2.0 */
    6.24 +    public static int searchByDescription(String name, Compute provider) {
    6.25 +        Iterator<Entry<String, String>> it = provider.getDataAndDescription().entrySet().iterator();
    6.26 +        for (int i = 0; it.hasNext(); i++) {
    6.27 +            if (name.equals(it.next().getValue())) {
    6.28 +                return i;
    6.29 +            }
    6.30 +        }
    6.31 +        return -1;
    6.32 +    }
    6.33 +    
    6.34 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/samples/growingparameters/src-impl/impl/Impl.java	Sat Jun 14 09:53:37 2008 +0200
     7.3 @@ -0,0 +1,38 @@
     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 +            @Override
    7.16 +            public List<String> getData() {
    7.17 +                return Collections.singletonList("Hello");
    7.18 +            }
    7.19 +        }
    7.20 +        int index1 = Support.searchByDescription("Hello", new ProviderWrittenAgainstVersion1());
    7.21 +        assert index1 == 0;
    7.22 +        int index2 = Support.searchByDescription("Unknown", new ProviderWrittenAgainstVersion1());
    7.23 +        assert index2 == -1;
    7.24 +
    7.25 +        
    7.26 +        class ProviderWrittenAgainstVersion2 extends Compute {
    7.27 +            @Override
    7.28 +            public List<String> getData() {
    7.29 +                return Collections.singletonList("Hello");
    7.30 +            }
    7.31 +
    7.32 +            @Override
    7.33 +            public Map<String, String> getDataAndDescription() {
    7.34 +                return Collections.singletonMap("Hello", "Says hello");
    7.35 +            }
    7.36 +        }
    7.37 +
    7.38 +        int index3 = Support.searchByDescription("Says hello", new ProviderWrittenAgainstVersion2());
    7.39 +        assert index3 == 0;
    7.40 +    }
    7.41 +}