001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002, 2003 Søren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package bak.pcj.adapter;
020:
021: import bak.pcj.Adapter;
022: import bak.pcj.list.AbstractBooleanList;
023: import bak.pcj.list.BooleanListIterator;
024: import bak.pcj.util.Exceptions;
025:
026: import java.util.List;
027:
028: /**
029: * This class represents adaptions of Java Collections Framework
030: * lists to primitive lists of boolean values.
031: * The adapter is implemented as a wrapper around the list.
032: * Thus, changes to the underlying list are reflected by this
033: * list and vice versa.
034: *
035: * <p>
036: * Adapters from JCF collections to primitive collections will
037: * fail if the JCF collection contains <tt>null</tt> values or
038: * values of the wrong class. However, adapters are not fast
039: * failing in the case that the underlying collection should
040: * contain illegal values. To implement fast failure would require
041: * every operation to check every element of the underlying
042: * collection before doing anything. Instead validation methods
043: * are provided. They can be called using the assertion facility
044: * in the client code:
045: * <pre>
046: * ListToBooleanListAdapter s;
047: * ...
048: * <b>assert</b> s.validate();
049: * </pre>
050: * or by letting the adapter throw an exception on illegal values:
051: * <pre>
052: * ListToBooleanListAdapter s;
053: * ...
054: * s.evalidate(); // Throws an exception on illegal values
055: * </pre>
056: * Either way, validation must be invoked directly by the client
057: * code.
058: *
059: * @author Søren Bak
060: * @version 1.2 20-08-2003 23:17
061: * @since 1.0
062: */
063: public class ListToBooleanListAdapter extends AbstractBooleanList {
064:
065: /** The underlying list. */
066: protected List list;
067:
068: /**
069: * Creates a new adaption of a list to a list of boolean
070: * values.
071: *
072: * @param list
073: * the underlying list. This list must
074: * consist of values of class
075: * {@link Boolean Boolean}. Otherwise a
076: * {@link ClassCastException ClassCastException}
077: * will be thrown by some methods.
078: *
079: * @throws NullPointerException
080: * if <tt>list</tt> is <tt>null</tt>.
081: */
082: public ListToBooleanListAdapter(List list) {
083: this (list, false);
084: }
085:
086: /**
087: * Creates a new adaption of a list to a list of boolean
088: * values. The list to adapt is optionally validated.
089: *
090: * @param list
091: * the underlying list. This collection must
092: * consist of values of class
093: * {@link Boolean Boolean}. Otherwise a
094: * {@link ClassCastException ClassCastException}
095: * will be thrown by some methods.
096: *
097: * @param validate
098: * indicates whether <tt>list</tt> should
099: * be checked for illegal values.
100: *
101: * @throws NullPointerException
102: * if <tt>list</tt> is <tt>null</tt>.
103: *
104: * @throws IllegalStateException
105: * if <tt>validate</tt> is <tt>true</tt> and
106: * <tt>list</tt> contains a <tt>null</tt> value
107: * or a value that is not of class
108: * {@link Boolean Boolean}.
109: */
110: public ListToBooleanListAdapter(List list, boolean validate) {
111: super ();
112: if (list == null)
113: Exceptions.nullArgument("list");
114: this .list = list;
115: if (validate)
116: evalidate();
117: }
118:
119: public void add(int index, boolean v) {
120: list.add(index, new Boolean(v));
121: }
122:
123: public boolean get(int index) {
124: return ((Boolean) list.get(index)).booleanValue();
125: }
126:
127: public BooleanListIterator listIterator(int index) {
128: return new ListIteratorToBooleanListIteratorAdapter(list
129: .listIterator(index));
130: }
131:
132: public boolean removeElementAt(int index) {
133: return ((Boolean) (list.remove(index))).booleanValue();
134: }
135:
136: public boolean set(int index, boolean v) {
137: return ((Boolean) list.set(index, new Boolean(v)))
138: .booleanValue();
139: }
140:
141: public int size() {
142: return list.size();
143: }
144:
145: /**
146: * Indicates whether the underlying list is valid for
147: * this adapter. For the underlying list to be valid, it
148: * can only contain {@link Boolean Boolean} values and no <tt>null</tt>
149: * values.
150: *
151: * @return <tt>true</tt> if the underlying list is
152: * valid; returns <tt>false</tt> otherwise.
153: */
154: public boolean validate() {
155: return Adapter.isBooleanAdaptable(list);
156: }
157:
158: /**
159: * Validates the list underlying this adapter and throws
160: * an exception if it is invalid. For the underlying list
161: * to be valid, it can only contain {@link Boolean Boolean}
162: * values and no <tt>null</tt> values.
163: *
164: * @throws IllegalStateException
165: * if the underlying list is invalid.
166: */
167: public void evalidate() {
168: if (!validate())
169: Exceptions.cannotAdapt("list");
170: }
171:
172: }
|