Repository navigation
Expand file tree
/
Copy pathWeissAbstractCollection.java
More file actions
201 lines (168 loc) · 5.13 KB
/
Copy pathWeissAbstractCollection.java
File metadata and controls
201 lines (168 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.util.Iterator;
/**
* AbstractCollection provides default implementations for
* some of the easy methods in the Collection interface.
*
* @param <T> type of items in the collection
*/
public abstract class WeissAbstractCollection<T> implements WeissCollection<T>
{
/**
* Tests if this collection is empty.
* @return true if the size of this collection is zero.
*/
public boolean isEmpty( )
{
return size( ) == 0;
}
/**
* Change the size of this collection to zero.
*/
public void clear( )
{
Iterator<T> itr = iterator( );
while( itr.hasNext( ) )
{
itr.next( );
itr.remove( );
}
}
/**
* Obtains a primitive array view of the collection.
* @return the primitive array view.
*/
public Object [ ] toArray( )
{
Object [ ] copy = new Object[ size( ) ];
int i = 0;
for( T val : this )
copy[ i++ ] = val;
return copy;
}
/**
* Returns an array copy of this collection as type E.
*
* @param <E> the other type for the new array
* @param arr an example array for using reflection to creates the array desired
* @return the array representation of this collection
*/
@SuppressWarnings("unchecked")
public <E> E [ ] toArray( E [ ] arr )
{
int size = size( );
if( arr.length < size )
arr = ( E [ ] )java.lang.reflect.Array.newInstance( arr.getClass( ).getComponentType( ), size );
else if( size < arr.length )
arr[ size ] = null;
Object [ ] result = arr;
Iterator<T> itr = iterator( );
for( int i = 0; i < size; i++ )
result[ i ] = itr.next( );
return arr;
}
/**
* Returns true if this collection contains x.
* If x is null, returns false.
* (This behavior may not always be appropriate.)
* @param x the item to search for.
* @return true if x is not null and is found in this collection.
*/
public boolean contains( Object x )
{
if( x == null )
return false;
for( T val : this )
if( x.equals( val ) )
return true;
return false;
}
/**
* Adds x to this collections.
* This default implementation always throws an exception.
* @param x the item to add
* @return whether or not something was added to the collection
* @throws UnsupportedOperationException always
*/
public boolean add( T x )
{
throw new UnsupportedOperationException( );
}
/**
* Removes non-null x from this collection.
* (This behavior may not always be appropriate.)
* @param x the item to remove.
* @return true if remove succeeds.
*/
public boolean remove( Object x )
{
if( x == null )
return false;
Iterator<T> itr = iterator( );
while( itr.hasNext( ) )
if( x.equals( itr.next( ) ) )
{
itr.remove( );
return true;
}
return false;
}
/**
* Return true if items in other collection
* are equal to items in this collection
* (same order, and same according to equals).
* @param other the item to compare with this one
* @return whether or not the two objects are equal
*/
public final boolean equals( Object other )
{
if( other == this )
return true;
if( ! ( other instanceof WeissCollection ) )
return false;
WeissCollection rhs = (WeissCollection) other;
if( size( ) != rhs.size( ) )
return false;
Iterator<T> lhsItr = this.iterator( );
Iterator rhsItr = rhs.iterator( );
while( lhsItr.hasNext( ) )
if( !isEqual( lhsItr.next( ), rhsItr.next( ) ) )
return false;
return true;
}
/**
* Return the hashCode.
* @return the hash code for this collection
*/
public final int hashCode( )
{
int hashVal = 1;
for( T obj : this )
hashVal = 31 * hashVal + ( obj == null ? 0 : obj.hashCode( ) );
return hashVal;
}
/**
* Return true if two objects are equal; works
* if objects can be null.
* @param lhs the first item
* @param rhs the second item
* @return whether or not the two objects are equal
*/
private boolean isEqual( Object lhs, Object rhs )
{
if( lhs == null )
return rhs == null;
return lhs.equals( rhs );
}
/**
* Return a string representation of this collection.
* @return the string representation of the collection
*/
public String toString( )
{
StringBuilder result = new StringBuilder( "[ " );
for( T obj : this )
result.append( obj + " " );
result.append( "]" );
return result.toString( );
}
}