1.1 --- a/samples/friendpackage/src/apipkg/AccessorImpl.java Fri Oct 24 12:07:34 2008 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,20 +0,0 @@
1.4 -package apipkg;
1.5 -
1.6 -import implpkg.Accessor;
1.7 -import javax.swing.event.ChangeListener;
1.8 -
1.9 -/** The bridge between api and impl package.
1.10 - *
1.11 - * @author Jaroslav Tulach
1.12 - */
1.13 -// BEGIN: design.less.friend.AccessorImpl
1.14 -final class AccessorImpl extends Accessor {
1.15 - protected Item newItem() {
1.16 - return new Item();
1.17 - }
1.18 -
1.19 - protected void addChangeListener(Item item, ChangeListener l) {
1.20 - item.addChangeListener(l);
1.21 - }
1.22 -}
1.23 -// END: design.less.friend.AccessorImpl
2.1 --- a/samples/friendpackage/src/apipkg/Item.java Fri Oct 24 12:07:34 2008 +0200
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,49 +0,0 @@
2.4 -package apipkg;
2.5 -
2.6 -import implpkg.Accessor;
2.7 -import javax.swing.event.ChangeEvent;
2.8 -import javax.swing.event.ChangeListener;
2.9 -
2.10 -/** Class in API that everyone can use.
2.11 - *
2.12 - * @author Jaroslav Tulach
2.13 - */
2.14 -// BEGIN: design.less.friend.Item
2.15 -public final class Item {
2.16 - private int value;
2.17 - private ChangeListener listener;
2.18 -
2.19 - /** Only friends can create instances. */
2.20 - Item() {
2.21 - }
2.22 -
2.23 - /** Anyone can change value of the item.
2.24 - */
2.25 - public void setValue(int newValue) {
2.26 - value = newValue;
2.27 - ChangeListener l = listener;
2.28 - if (l != null) {
2.29 - l.stateChanged(new ChangeEvent(this));
2.30 - }
2.31 - }
2.32 -
2.33 - /** Anyone can get the value of the item.
2.34 - */
2.35 - public int getValue() {
2.36 - return value;
2.37 - }
2.38 -
2.39 - /** Only friends can listen to changes.
2.40 - */
2.41 - void addChangeListener(ChangeListener l) {
2.42 - assert listener == null;
2.43 - listener = l;
2.44 - }
2.45 -// FINISH: design.less.friend.Item
2.46 -
2.47 - // BEGIN: design.less.friend.Item.static
2.48 - static {
2.49 - Accessor.setDefault(new AccessorImpl());
2.50 - }
2.51 - // END: design.less.friend.Item.static
2.52 -}
3.1 --- a/samples/friendpackage/src/implpkg/Accessor.java Fri Oct 24 12:07:34 2008 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,60 +0,0 @@
3.4 -/*
3.5 - * Sun Public License Notice
3.6 - *
3.7 - * The contents of this file are subject to the Sun Public License
3.8 - * Version 1.0 (the "License"). You may not use this file except in
3.9 - * compliance with the License. A copy of the License is available at
3.10 - * http://www.sun.com/
3.11 - *
3.12 - * The Original Code is NetBeans. The Initial Developer of the Original
3.13 - * Code is Jaroslav Tulach. Portions Copyright 2007 Jaroslav Tulach.
3.14 - * All Rights Reserved.
3.15 - */
3.16 -
3.17 -package implpkg;
3.18 -
3.19 -import apipkg.Item;
3.20 -import javax.swing.event.ChangeListener;
3.21 -
3.22 -/**
3.23 - *
3.24 - * @author Jaroslav Tulach
3.25 - */
3.26 -// BEGIN: design.less.friend.Accessor
3.27 -public abstract class Accessor {
3.28 - private static volatile Accessor DEFAULT;
3.29 - public static Accessor getDefault() {
3.30 - Accessor a = DEFAULT;
3.31 - if (a == null) {
3.32 - throw new IllegalStateException("Something is wrong: " + a);
3.33 - }
3.34 - return a;
3.35 - }
3.36 -
3.37 - public static void setDefault(Accessor accessor) {
3.38 - if (DEFAULT != null) {
3.39 - throw new IllegalStateException();
3.40 - }
3.41 - DEFAULT = accessor;
3.42 - }
3.43 -
3.44 - public Accessor() {
3.45 - }
3.46 -
3.47 - protected abstract Item newItem();
3.48 - protected abstract void addChangeListener(Item item, ChangeListener l);
3.49 -// FINISH: design.less.friend.Accessor
3.50 -
3.51 - // BEGIN: design.less.friend.InitAPI
3.52 - private static final Class<?> INIT_API_CLASS = loadClass(Item.class.getName());
3.53 - private static Class<?> loadClass(String name) {
3.54 - try {
3.55 - return Class.forName(
3.56 - name, true, Accessor.class.getClassLoader()
3.57 - );
3.58 - } catch (Exception ex) {
3.59 - throw new RuntimeException(ex);
3.60 - }
3.61 - }
3.62 - // END: design.less.friend.InitAPI
3.63 -}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/samples/friendpackage/src/org/apidesign/friendpackage/api/AccessorImpl.java Fri Oct 24 21:15:50 2008 +0200
4.3 @@ -0,0 +1,20 @@
4.4 +package org.apidesign.friendpackage.api;
4.5 +
4.6 +import javax.swing.event.ChangeListener;
4.7 +import org.apidesign.friendpackage.impl.Accessor;
4.8 +
4.9 +/** The bridge between api and impl package.
4.10 + *
4.11 + * @author Jaroslav Tulach
4.12 + */
4.13 +// BEGIN: design.less.friend.AccessorImpl
4.14 +final class AccessorImpl extends Accessor {
4.15 + protected Item newItem() {
4.16 + return new Item();
4.17 + }
4.18 +
4.19 + protected void addChangeListener(Item item, ChangeListener l) {
4.20 + item.addChangeListener(l);
4.21 + }
4.22 +}
4.23 +// END: design.less.friend.AccessorImpl
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/samples/friendpackage/src/org/apidesign/friendpackage/api/Item.java Fri Oct 24 21:15:50 2008 +0200
5.3 @@ -0,0 +1,49 @@
5.4 +package org.apidesign.friendpackage.api;
5.5 +
5.6 +import javax.swing.event.ChangeEvent;
5.7 +import javax.swing.event.ChangeListener;
5.8 +import org.apidesign.friendpackage.impl.Accessor;
5.9 +
5.10 +/** Class in API that everyone can use.
5.11 + *
5.12 + * @author Jaroslav Tulach
5.13 + */
5.14 +// BEGIN: design.less.friend.Item
5.15 +public final class Item {
5.16 + private int value;
5.17 + private ChangeListener listener;
5.18 +
5.19 + /** Only friends can create instances. */
5.20 + Item() {
5.21 + }
5.22 +
5.23 + /** Anyone can change value of the item.
5.24 + */
5.25 + public void setValue(int newValue) {
5.26 + value = newValue;
5.27 + ChangeListener l = listener;
5.28 + if (l != null) {
5.29 + l.stateChanged(new ChangeEvent(this));
5.30 + }
5.31 + }
5.32 +
5.33 + /** Anyone can get the value of the item.
5.34 + */
5.35 + public int getValue() {
5.36 + return value;
5.37 + }
5.38 +
5.39 + /** Only friends can listen to changes.
5.40 + */
5.41 + void addChangeListener(ChangeListener l) {
5.42 + assert listener == null;
5.43 + listener = l;
5.44 + }
5.45 +// FINISH: design.less.friend.Item
5.46 +
5.47 + // BEGIN: design.less.friend.Item.static
5.48 + static {
5.49 + Accessor.setDefault(new AccessorImpl());
5.50 + }
5.51 + // END: design.less.friend.Item.static
5.52 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/samples/friendpackage/src/org/apidesign/friendpackage/impl/Accessor.java Fri Oct 24 21:15:50 2008 +0200
6.3 @@ -0,0 +1,47 @@
6.4 +package org.apidesign.friendpackage.impl;
6.5 +
6.6 +import javax.swing.event.ChangeListener;
6.7 +import org.apidesign.friendpackage.api.Item;
6.8 +
6.9 +/**
6.10 + *
6.11 + * @author Jaroslav Tulach
6.12 + */
6.13 +// BEGIN: design.less.friend.Accessor
6.14 +public abstract class Accessor {
6.15 + private static volatile Accessor DEFAULT;
6.16 + public static Accessor getDefault() {
6.17 + Accessor a = DEFAULT;
6.18 + if (a == null) {
6.19 + throw new IllegalStateException("Something is wrong: " + a);
6.20 + }
6.21 + return a;
6.22 + }
6.23 +
6.24 + public static void setDefault(Accessor accessor) {
6.25 + if (DEFAULT != null) {
6.26 + throw new IllegalStateException();
6.27 + }
6.28 + DEFAULT = accessor;
6.29 + }
6.30 +
6.31 + public Accessor() {
6.32 + }
6.33 +
6.34 + protected abstract Item newItem();
6.35 + protected abstract void addChangeListener(Item item, ChangeListener l);
6.36 +// FINISH: design.less.friend.Accessor
6.37 +
6.38 + // BEGIN: design.less.friend.InitAPI
6.39 + private static final Class<?> INIT_API_CLASS = loadClass(Item.class.getName());
6.40 + private static Class<?> loadClass(String name) {
6.41 + try {
6.42 + return Class.forName(
6.43 + name, true, Accessor.class.getClassLoader()
6.44 + );
6.45 + } catch (Exception ex) {
6.46 + throw new RuntimeException(ex);
6.47 + }
6.48 + }
6.49 + // END: design.less.friend.InitAPI
6.50 +}
7.1 --- a/samples/friendpackage/test/implpkg/AccessorTest.java Fri Oct 24 12:07:34 2008 +0200
7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
7.3 @@ -1,55 +0,0 @@
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 Jaroslav Tulach. Portions Copyright 2007 Jaroslav Tulach.
7.14 - * 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 - public void testGetTheItemAttachListenerChangeValue() {
7.39 - // BEGIN: design.less.friend.use
7.40 - Item item = Accessor.getDefault().newItem();
7.41 - assertNotNull("Some item is really created", item);
7.42 -
7.43 - Accessor.getDefault().addChangeListener(item, this);
7.44 - // END: design.less.friend.use
7.45 -
7.46 - item.setValue(10);
7.47 - assertEquals("Value is 10", 10, item.getValue());
7.48 - cnt = 0;
7.49 - item.setValue(7);
7.50 - assertEquals("Now it is 7", 7, item.getValue());
7.51 -
7.52 - assertEquals("There was one change", 1, cnt);
7.53 - }
7.54 -
7.55 - public void stateChanged(ChangeEvent e) {
7.56 - cnt++;
7.57 - }
7.58 -}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/samples/friendpackage/test/org/apidesign/friendpackage/impl/AccessorTest.java Fri Oct 24 21:15:50 2008 +0200
8.3 @@ -0,0 +1,54 @@
8.4 +/*
8.5 + * Sun Public License Notice
8.6 + *
8.7 + * The contents of this file are subject to the Sun Public License
8.8 + * Version 1.0 (the "License"). You may not use this file except in
8.9 + * compliance with the License. A copy of the License is available at
8.10 + * http://www.sun.com/
8.11 + *
8.12 + * The Original Code is NetBeans. The Initial Developer of the Original
8.13 + * Code is Jaroslav Tulach. Portions Copyright 2007 Jaroslav Tulach.
8.14 + * All Rights Reserved.
8.15 + */
8.16 +
8.17 +package org.apidesign.friendpackage.impl;
8.18 +
8.19 +import javax.swing.event.ChangeEvent;
8.20 +import javax.swing.event.ChangeListener;
8.21 +import junit.framework.TestCase;
8.22 +import org.apidesign.friendpackage.api.Item;
8.23 +
8.24 +/**
8.25 + *
8.26 + * @author Jaroslav Tulach
8.27 + */
8.28 +public class AccessorTest extends TestCase
8.29 +implements ChangeListener {
8.30 +
8.31 + private int cnt;
8.32 +
8.33 + public AccessorTest(String testName) {
8.34 + super(testName);
8.35 + }
8.36 +
8.37 + public void testGetTheItemAttachListenerChangeValue() {
8.38 + // BEGIN: design.less.friend.use
8.39 + Item item = Accessor.getDefault().newItem();
8.40 + assertNotNull("Some item is really created", item);
8.41 +
8.42 + Accessor.getDefault().addChangeListener(item, this);
8.43 + // END: design.less.friend.use
8.44 +
8.45 + item.setValue(10);
8.46 + assertEquals("Value is 10", 10, item.getValue());
8.47 + cnt = 0;
8.48 + item.setValue(7);
8.49 + assertEquals("Now it is 7", 7, item.getValue());
8.50 +
8.51 + assertEquals("There was one change", 1, cnt);
8.52 + }
8.53 +
8.54 + public void stateChanged(ChangeEvent e) {
8.55 + cnt++;
8.56 + }
8.57 +}