Adding friend access example
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:50:27 +0200
changeset 032b39aaa68f0
child 1 de3e5a6c990b
Adding friend access example
samples/friendpackage/nbproject/genfiles.properties
samples/friendpackage/nbproject/project.properties
samples/friendpackage/nbproject/project.xml
samples/friendpackage/src/apipkg/AccessorImpl.java
samples/friendpackage/src/apipkg/Item.java
samples/friendpackage/src/implpkg/Accessor.java
samples/friendpackage/test/implpkg/AccessorTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/friendpackage/nbproject/genfiles.properties	Sat Jun 14 09:50:27 2008 +0200
     1.3 @@ -0,0 +1,8 @@
     1.4 +build.xml.data.CRC32=b1c774b2
     1.5 +build.xml.script.CRC32=fa64c0b4
     1.6 +build.xml.stylesheet.CRC32=d5b6853a
     1.7 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
     1.8 +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
     1.9 +nbproject/build-impl.xml.data.CRC32=b1c774b2
    1.10 +nbproject/build-impl.xml.script.CRC32=d7cec00a
    1.11 +nbproject/build-impl.xml.stylesheet.CRC32=3a518818
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/friendpackage/nbproject/project.properties	Sat Jun 14 09:50:27 2008 +0200
     2.3 @@ -0,0 +1,53 @@
     2.4 +build.classes.dir=${build.dir}/classes
     2.5 +build.classes.excludes=**/*.java,**/*.form
     2.6 +# This directory is removed when the project is cleaned:
     2.7 +build.dir=build
     2.8 +build.generated.dir=${build.dir}/generated
     2.9 +# Only compile against the classpath explicitly listed here:
    2.10 +build.sysclasspath=ignore
    2.11 +build.test.classes.dir=${build.dir}/test/classes
    2.12 +build.test.results.dir=${build.dir}/test/results
    2.13 +debug.classpath=\
    2.14 +    ${run.classpath}
    2.15 +debug.test.classpath=\
    2.16 +    ${run.test.classpath}
    2.17 +# This directory is removed when the project is cleaned:
    2.18 +dist.dir=dist
    2.19 +dist.jar=${dist.dir}/friendpackage.jar
    2.20 +dist.javadoc.dir=${dist.dir}/javadoc
    2.21 +jar.compress=false
    2.22 +javac.classpath=
    2.23 +# Space-separated list of extra javac options
    2.24 +javac.compilerargs=
    2.25 +javac.deprecation=false
    2.26 +javac.source=1.5
    2.27 +javac.target=1.5
    2.28 +javac.test.classpath=\
    2.29 +    ${javac.classpath}:\
    2.30 +    ${build.classes.dir}:\
    2.31 +    ${libs.junit.classpath}
    2.32 +javadoc.additionalparam=
    2.33 +javadoc.author=false
    2.34 +javadoc.encoding=
    2.35 +javadoc.noindex=false
    2.36 +javadoc.nonavbar=false
    2.37 +javadoc.notree=false
    2.38 +javadoc.private=false
    2.39 +javadoc.splitindex=true
    2.40 +javadoc.use=true
    2.41 +javadoc.version=false
    2.42 +javadoc.windowtitle=
    2.43 +meta.inf.dir=${src.dir}/META-INF
    2.44 +platform.active=default_platform
    2.45 +run.classpath=\
    2.46 +    ${javac.classpath}:\
    2.47 +    ${build.classes.dir}
    2.48 +# Space-separated list of JVM arguments used when running the project
    2.49 +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
    2.50 +# or test-sys-prop.name=value to set system properties for unit tests):
    2.51 +run.jvmargs=
    2.52 +run.test.classpath=\
    2.53 +    ${javac.test.classpath}:\
    2.54 +    ${build.test.classes.dir}
    2.55 +src.dir=src
    2.56 +test.src.dir=test
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/friendpackage/nbproject/project.xml	Sat Jun 14 09:50:27 2008 +0200
     3.3 @@ -0,0 +1,16 @@
     3.4 +<?xml version="1.0" encoding="UTF-8"?>
     3.5 +<project xmlns="http://www.netbeans.org/ns/project/1">
     3.6 +    <type>org.netbeans.modules.java.j2seproject</type>
     3.7 +    <configuration>
     3.8 +        <data xmlns="http://www.netbeans.org/ns/j2se-project/3">
     3.9 +            <name>friendpackage</name>
    3.10 +            <minimum-ant-version>1.6.5</minimum-ant-version>
    3.11 +            <source-roots>
    3.12 +                <root id="src.dir"/>
    3.13 +            </source-roots>
    3.14 +            <test-roots>
    3.15 +                <root id="test.src.dir"/>
    3.16 +            </test-roots>
    3.17 +        </data>
    3.18 +    </configuration>
    3.19 +</project>
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/friendpackage/src/apipkg/AccessorImpl.java	Sat Jun 14 09:50:27 2008 +0200
     4.3 @@ -0,0 +1,32 @@
     4.4 +/*
     4.5 + *                 Sun Public License Notice
     4.6 + * 
     4.7 + * The contents of this file are subject to the Sun Public License
     4.8 + * Version 1.0 (the "License"). You may not use this file except in
     4.9 + * compliance with the License. A copy of the License is available at
    4.10 + * http://www.sun.com/
    4.11 + * 
    4.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    4.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun
    4.14 + * Microsystems, Inc. All Rights Reserved.
    4.15 + */
    4.16 +
    4.17 +package apipkg;
    4.18 +
    4.19 +import implpkg.Accessor;
    4.20 +import javax.swing.event.ChangeEvent;
    4.21 +import javax.swing.event.ChangeListener;
    4.22 +
    4.23 +/** The bridge between api and impl package.
    4.24 + *
    4.25 + * @author Jaroslav Tulach
    4.26 + */
    4.27 +final class AccessorImpl extends Accessor {
    4.28 +    protected Item newItem() {
    4.29 +        return new Item();
    4.30 +    }
    4.31 +
    4.32 +    protected void addChangeListener(Item item, ChangeListener l) {
    4.33 +        item.addChangeListener(l);
    4.34 +    }
    4.35 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/friendpackage/src/apipkg/Item.java	Sat Jun 14 09:50:27 2008 +0200
     5.3 @@ -0,0 +1,59 @@
     5.4 +/*
     5.5 + *                 Sun Public License Notice
     5.6 + * 
     5.7 + * The contents of this file are subject to the Sun Public License
     5.8 + * Version 1.0 (the "License"). You may not use this file except in
     5.9 + * compliance with the License. A copy of the License is available at
    5.10 + * http://www.sun.com/
    5.11 + * 
    5.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    5.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun
    5.14 + * Microsystems, Inc. All Rights Reserved.
    5.15 + */
    5.16 +
    5.17 +package apipkg;
    5.18 +
    5.19 +import implpkg.Accessor;
    5.20 +import javax.swing.event.ChangeEvent;
    5.21 +import javax.swing.event.ChangeListener;
    5.22 +
    5.23 +/** Class in API that everyone can use.
    5.24 + *
    5.25 + * @author Jaroslav Tulach
    5.26 + */
    5.27 +public final class Item {
    5.28 +    private int value;
    5.29 +    private ChangeListener listener;
    5.30 +    
    5.31 +    static {
    5.32 +        Accessor.DEFAULT = new AccessorImpl();
    5.33 +    }
    5.34 +    
    5.35 +    /** Contructor for friends */
    5.36 +    Item() {
    5.37 +    }
    5.38 +    
    5.39 +    /** Anyone can value of the item. At least if it can get a reference to it.
    5.40 +     */
    5.41 +    public void setValue(int x) {
    5.42 +        value = x;
    5.43 +        ChangeListener l = listener;
    5.44 +        if (l != null) {
    5.45 +            l.stateChanged(new ChangeEvent(this));
    5.46 +        }
    5.47 +    }
    5.48 +    
    5.49 +    /** Anyone can get the value of the item. 
    5.50 +     */
    5.51 +    public int getValue() {
    5.52 +        return value;
    5.53 +    }
    5.54 +    
    5.55 +    /** Only the impl package can listen.
    5.56 +     */
    5.57 +    void addChangeListener(ChangeListener l) {
    5.58 +        assert listener == null;
    5.59 +        listener = l;
    5.60 +    }
    5.61 +    
    5.62 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/friendpackage/src/implpkg/Accessor.java	Sat Jun 14 09:50:27 2008 +0200
     6.3 @@ -0,0 +1,38 @@
     6.4 +/*
     6.5 + *                 Sun Public License Notice
     6.6 + * 
     6.7 + * The contents of this file are subject to the Sun Public License
     6.8 + * Version 1.0 (the "License"). You may not use this file except in
     6.9 + * compliance with the License. A copy of the License is available at
    6.10 + * http://www.sun.com/
    6.11 + * 
    6.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    6.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun
    6.14 + * Microsystems, Inc. All Rights Reserved.
    6.15 + */
    6.16 +
    6.17 +package implpkg;
    6.18 +
    6.19 +import apipkg.Item;
    6.20 +import javax.swing.event.ChangeListener;
    6.21 +
    6.22 +/**
    6.23 + *
    6.24 + * @author Jaroslav Tulach
    6.25 + */
    6.26 +public abstract class Accessor {
    6.27 +    public static Accessor DEFAULT;
    6.28 +    static {
    6.29 +        try {
    6.30 +            Class.forName(Item.class.getName(), true, Item.class.getClassLoader());
    6.31 +        } catch (Exception ex) {
    6.32 +            ex.printStackTrace();
    6.33 +        }
    6.34 +    }
    6.35 +    
    6.36 +    public Accessor() {
    6.37 +    }
    6.38 +
    6.39 +    protected abstract Item newItem();
    6.40 +    protected abstract void addChangeListener(Item item, ChangeListener l);
    6.41 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/samples/friendpackage/test/implpkg/AccessorTest.java	Sat Jun 14 09:50:27 2008 +0200
     7.3 @@ -0,0 +1,59 @@
     7.4 +/*
     7.5 + *                 Sun Public License Notice
     7.6 + * 
     7.7 + * The contents of this file are subject to the Sun Public License
     7.8 + * Version 1.0 (the "License"). You may not use this file except in
     7.9 + * compliance with the License. A copy of the License is available at
    7.10 + * http://www.sun.com/
    7.11 + * 
    7.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    7.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun
    7.14 + * Microsystems, Inc. All Rights Reserved.
    7.15 + */
    7.16 +
    7.17 +package implpkg;
    7.18 +
    7.19 +import javax.swing.event.ChangeEvent;
    7.20 +import junit.framework.TestCase;
    7.21 +import junit.framework.*;
    7.22 +import apipkg.Item;
    7.23 +import javax.swing.event.ChangeListener;
    7.24 +
    7.25 +/**
    7.26 + *
    7.27 + * @author Jaroslav Tulach
    7.28 + */
    7.29 +public class AccessorTest extends TestCase 
    7.30 +implements ChangeListener {
    7.31 +
    7.32 +    private int cnt;
    7.33 +    
    7.34 +    public AccessorTest(String testName) {
    7.35 +        super(testName);
    7.36 +    }
    7.37 +
    7.38 +    protected void setUp() throws Exception {
    7.39 +    }
    7.40 +
    7.41 +    protected void tearDown() throws Exception {
    7.42 +    }
    7.43 +    
    7.44 +    public void testGetTheItemAttachListenerChangeValue() {
    7.45 +        Item item = Accessor.DEFAULT.newItem();
    7.46 +        assertNotNull("Some item is really created", item);
    7.47 +        
    7.48 +        Accessor.DEFAULT.addChangeListener(item, this);
    7.49 +        
    7.50 +        item.setValue(10);
    7.51 +        assertEquals("Value is 10", 10, item.getValue());
    7.52 +        cnt = 0;
    7.53 +        item.setValue(7);
    7.54 +        assertEquals("Now it is 7", 7, item.getValue());
    7.55 +        
    7.56 +        assertEquals("There was one change", 1, cnt);
    7.57 +    }
    7.58 +
    7.59 +    public void stateChanged(ChangeEvent e) {
    7.60 +        cnt++;
    7.61 +    }
    7.62 +}