# HG changeset patch # User Jaroslav Tulach # Date 1224875750 -7200 # Node ID e1186601a720a34e2d202a099c55b0794363428e # Parent ac16aae50d58c5d29df0b8bf5a509e39d36b24d9 Moving into real packages diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/src/apipkg/AccessorImpl.java --- a/samples/friendpackage/src/apipkg/AccessorImpl.java Fri Oct 24 12:07:34 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -package apipkg; - -import implpkg.Accessor; -import javax.swing.event.ChangeListener; - -/** The bridge between api and impl package. - * - * @author Jaroslav Tulach - */ -// BEGIN: design.less.friend.AccessorImpl -final class AccessorImpl extends Accessor { - protected Item newItem() { - return new Item(); - } - - protected void addChangeListener(Item item, ChangeListener l) { - item.addChangeListener(l); - } -} -// END: design.less.friend.AccessorImpl diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/src/apipkg/Item.java --- a/samples/friendpackage/src/apipkg/Item.java Fri Oct 24 12:07:34 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -package apipkg; - -import implpkg.Accessor; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; - -/** Class in API that everyone can use. - * - * @author Jaroslav Tulach - */ -// BEGIN: design.less.friend.Item -public final class Item { - private int value; - private ChangeListener listener; - - /** Only friends can create instances. */ - Item() { - } - - /** Anyone can change value of the item. - */ - public void setValue(int newValue) { - value = newValue; - ChangeListener l = listener; - if (l != null) { - l.stateChanged(new ChangeEvent(this)); - } - } - - /** Anyone can get the value of the item. - */ - public int getValue() { - return value; - } - - /** Only friends can listen to changes. - */ - void addChangeListener(ChangeListener l) { - assert listener == null; - listener = l; - } -// FINISH: design.less.friend.Item - - // BEGIN: design.less.friend.Item.static - static { - Accessor.setDefault(new AccessorImpl()); - } - // END: design.less.friend.Item.static -} diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/src/implpkg/Accessor.java --- a/samples/friendpackage/src/implpkg/Accessor.java Fri Oct 24 12:07:34 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -/* - * Sun Public License Notice - * - * The contents of this file are subject to the Sun Public License - * Version 1.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://www.sun.com/ - * - * The Original Code is NetBeans. The Initial Developer of the Original - * Code is Jaroslav Tulach. Portions Copyright 2007 Jaroslav Tulach. - * All Rights Reserved. - */ - -package implpkg; - -import apipkg.Item; -import javax.swing.event.ChangeListener; - -/** - * - * @author Jaroslav Tulach - */ -// BEGIN: design.less.friend.Accessor -public abstract class Accessor { - private static volatile Accessor DEFAULT; - public static Accessor getDefault() { - Accessor a = DEFAULT; - if (a == null) { - throw new IllegalStateException("Something is wrong: " + a); - } - return a; - } - - public static void setDefault(Accessor accessor) { - if (DEFAULT != null) { - throw new IllegalStateException(); - } - DEFAULT = accessor; - } - - public Accessor() { - } - - protected abstract Item newItem(); - protected abstract void addChangeListener(Item item, ChangeListener l); -// FINISH: design.less.friend.Accessor - - // BEGIN: design.less.friend.InitAPI - private static final Class INIT_API_CLASS = loadClass(Item.class.getName()); - private static Class loadClass(String name) { - try { - return Class.forName( - name, true, Accessor.class.getClassLoader() - ); - } catch (Exception ex) { - throw new RuntimeException(ex); - } - } - // END: design.less.friend.InitAPI -} diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/src/org/apidesign/friendpackage/api/AccessorImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/src/org/apidesign/friendpackage/api/AccessorImpl.java Fri Oct 24 21:15:50 2008 +0200 @@ -0,0 +1,20 @@ +package org.apidesign.friendpackage.api; + +import javax.swing.event.ChangeListener; +import org.apidesign.friendpackage.impl.Accessor; + +/** The bridge between api and impl package. + * + * @author Jaroslav Tulach + */ +// BEGIN: design.less.friend.AccessorImpl +final class AccessorImpl extends Accessor { + protected Item newItem() { + return new Item(); + } + + protected void addChangeListener(Item item, ChangeListener l) { + item.addChangeListener(l); + } +} +// END: design.less.friend.AccessorImpl diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/src/org/apidesign/friendpackage/api/Item.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/src/org/apidesign/friendpackage/api/Item.java Fri Oct 24 21:15:50 2008 +0200 @@ -0,0 +1,49 @@ +package org.apidesign.friendpackage.api; + +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import org.apidesign.friendpackage.impl.Accessor; + +/** Class in API that everyone can use. + * + * @author Jaroslav Tulach + */ +// BEGIN: design.less.friend.Item +public final class Item { + private int value; + private ChangeListener listener; + + /** Only friends can create instances. */ + Item() { + } + + /** Anyone can change value of the item. + */ + public void setValue(int newValue) { + value = newValue; + ChangeListener l = listener; + if (l != null) { + l.stateChanged(new ChangeEvent(this)); + } + } + + /** Anyone can get the value of the item. + */ + public int getValue() { + return value; + } + + /** Only friends can listen to changes. + */ + void addChangeListener(ChangeListener l) { + assert listener == null; + listener = l; + } +// FINISH: design.less.friend.Item + + // BEGIN: design.less.friend.Item.static + static { + Accessor.setDefault(new AccessorImpl()); + } + // END: design.less.friend.Item.static +} diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/src/org/apidesign/friendpackage/impl/Accessor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/src/org/apidesign/friendpackage/impl/Accessor.java Fri Oct 24 21:15:50 2008 +0200 @@ -0,0 +1,47 @@ +package org.apidesign.friendpackage.impl; + +import javax.swing.event.ChangeListener; +import org.apidesign.friendpackage.api.Item; + +/** + * + * @author Jaroslav Tulach + */ +// BEGIN: design.less.friend.Accessor +public abstract class Accessor { + private static volatile Accessor DEFAULT; + public static Accessor getDefault() { + Accessor a = DEFAULT; + if (a == null) { + throw new IllegalStateException("Something is wrong: " + a); + } + return a; + } + + public static void setDefault(Accessor accessor) { + if (DEFAULT != null) { + throw new IllegalStateException(); + } + DEFAULT = accessor; + } + + public Accessor() { + } + + protected abstract Item newItem(); + protected abstract void addChangeListener(Item item, ChangeListener l); +// FINISH: design.less.friend.Accessor + + // BEGIN: design.less.friend.InitAPI + private static final Class INIT_API_CLASS = loadClass(Item.class.getName()); + private static Class loadClass(String name) { + try { + return Class.forName( + name, true, Accessor.class.getClassLoader() + ); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + // END: design.less.friend.InitAPI +} diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/test/implpkg/AccessorTest.java --- a/samples/friendpackage/test/implpkg/AccessorTest.java Fri Oct 24 12:07:34 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/* - * Sun Public License Notice - * - * The contents of this file are subject to the Sun Public License - * Version 1.0 (the "License"). You may not use this file except in - * compliance with the License. A copy of the License is available at - * http://www.sun.com/ - * - * The Original Code is NetBeans. The Initial Developer of the Original - * Code is Jaroslav Tulach. Portions Copyright 2007 Jaroslav Tulach. - * All Rights Reserved. - */ - -package implpkg; - -import javax.swing.event.ChangeEvent; -import junit.framework.TestCase; -import junit.framework.*; -import apipkg.Item; -import javax.swing.event.ChangeListener; - -/** - * - * @author Jaroslav Tulach - */ -public class AccessorTest extends TestCase -implements ChangeListener { - - private int cnt; - - public AccessorTest(String testName) { - super(testName); - } - - public void testGetTheItemAttachListenerChangeValue() { - // BEGIN: design.less.friend.use - Item item = Accessor.getDefault().newItem(); - assertNotNull("Some item is really created", item); - - Accessor.getDefault().addChangeListener(item, this); - // END: design.less.friend.use - - item.setValue(10); - assertEquals("Value is 10", 10, item.getValue()); - cnt = 0; - item.setValue(7); - assertEquals("Now it is 7", 7, item.getValue()); - - assertEquals("There was one change", 1, cnt); - } - - public void stateChanged(ChangeEvent e) { - cnt++; - } -} diff -r ac16aae50d58 -r e1186601a720 samples/friendpackage/test/org/apidesign/friendpackage/impl/AccessorTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/test/org/apidesign/friendpackage/impl/AccessorTest.java Fri Oct 24 21:15:50 2008 +0200 @@ -0,0 +1,54 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Jaroslav Tulach. Portions Copyright 2007 Jaroslav Tulach. + * All Rights Reserved. + */ + +package org.apidesign.friendpackage.impl; + +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import junit.framework.TestCase; +import org.apidesign.friendpackage.api.Item; + +/** + * + * @author Jaroslav Tulach + */ +public class AccessorTest extends TestCase +implements ChangeListener { + + private int cnt; + + public AccessorTest(String testName) { + super(testName); + } + + public void testGetTheItemAttachListenerChangeValue() { + // BEGIN: design.less.friend.use + Item item = Accessor.getDefault().newItem(); + assertNotNull("Some item is really created", item); + + Accessor.getDefault().addChangeListener(item, this); + // END: design.less.friend.use + + item.setValue(10); + assertEquals("Value is 10", 10, item.getValue()); + cnt = 0; + item.setValue(7); + assertEquals("Now it is 7", 7, item.getValue()); + + assertEquals("There was one change", 1, cnt); + } + + public void stateChanged(ChangeEvent e) { + cnt++; + } +}