KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > adapter > ListToFloatListAdapter


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002, 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.adapter;
20
21 import bak.pcj.Adapter;
22 import bak.pcj.list.AbstractFloatList;
23 import bak.pcj.list.FloatListIterator;
24 import bak.pcj.util.Exceptions;
25
26 import java.util.List JavaDoc;
27
28 /**
29  * This class represents adaptions of Java Collections Framework
30  * lists to primitive lists of float values.
31  * The adapter is implemented as a wrapper around the list.
32  * Thus, changes to the underlying list are reflected by this
33  * list and vice versa.
34  *
35  * <p>
36  * Adapters from JCF collections to primitive collections will
37  * fail if the JCF collection contains <tt>null</tt> values or
38  * values of the wrong class. However, adapters are not fast
39  * failing in the case that the underlying collection should
40  * contain illegal values. To implement fast failure would require
41  * every operation to check every element of the underlying
42  * collection before doing anything. Instead validation methods
43  * are provided. They can be called using the assertion facility
44  * in the client code:
45  * <pre>
46  * ListToFloatListAdapter s;
47  * ...
48  * <b>assert</b> s.validate();
49  * </pre>
50  * or by letting the adapter throw an exception on illegal values:
51  * <pre>
52  * ListToFloatListAdapter s;
53  * ...
54  * s.evalidate(); // Throws an exception on illegal values
55  * </pre>
56  * Either way, validation must be invoked directly by the client
57  * code.
58  *
59  * @author S&oslash;ren Bak
60  * @version 1.2 20-08-2003 23:17
61  * @since 1.0
62  */

63 public class ListToFloatListAdapter extends AbstractFloatList {
64
65     /** The underlying list. */
66     protected List list;
67
68     /**
69      * Creates a new adaption of a list to a list of float
70      * values.
71      *
72      * @param list
73      * the underlying list. This list must
74      * consist of values of class
75      * {@link Float Float}. Otherwise a
76      * {@link ClassCastException ClassCastException}
77      * will be thrown by some methods.
78      *
79      * @throws NullPointerException
80      * if <tt>list</tt> is <tt>null</tt>.
81      */

82     public ListToFloatListAdapter(List list) {
83         this(list, false);
84     }
85
86     /**
87      * Creates a new adaption of a list to a list of float
88      * values. The list to adapt is optionally validated.
89      *
90      * @param list
91      * the underlying list. This collection must
92      * consist of values of class
93      * {@link Float Float}. Otherwise a
94      * {@link ClassCastException ClassCastException}
95      * will be thrown by some methods.
96      *
97      * @param validate
98      * indicates whether <tt>list</tt> should
99      * 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 Float Float}.
109      */

110     public ListToFloatListAdapter(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, float v)
120     { list.add(index, new Float JavaDoc(v)); }
121
122     public float get(int index)
123     { return ((Float JavaDoc)list.get(index)).floatValue(); }
124
125     public FloatListIterator listIterator(int index)
126     { return new ListIteratorToFloatListIteratorAdapter(list.listIterator(index)); }
127
128     public float removeElementAt(int index)
129     { return ((Float JavaDoc)(list.remove(index))).floatValue(); }
130
131     public float set(int index, float v)
132     { return ((Float JavaDoc)list.set(index, new Float JavaDoc(v))).floatValue(); }
133
134     public int size()
135     { return list.size(); }
136
137     /**
138      * Indicates whether the underlying list is valid for
139      * this adapter. For the underlying list to be valid, it
140      * can only contain {@link Float Float} values and no <tt>null</tt>
141      * values.
142      *
143      * @return <tt>true</tt> if the underlying list is
144      * valid; returns <tt>false</tt> otherwise.
145      */

146     public boolean validate()
147     { return Adapter.isFloatAdaptable(list); }
148
149     /**
150      * Validates the list underlying this adapter and throws
151      * an exception if it is invalid. For the underlying list
152      * to be valid, it can only contain {@link Float Float}
153      * values and no <tt>null</tt> values.
154      *
155      * @throws IllegalStateException
156      * if the underlying list is invalid.
157      */

158     public void evalidate() {
159         if (!validate())
160             Exceptions.cannotAdapt("list");
161     }
162
163 }
Popular Tags