-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyCollection.java
73 lines (64 loc) · 1.88 KB
/
MyCollection.java
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
package ru.kpfu.itis.barakhov.units;
import java.util.AbstractCollection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
public class MyCollection<E> extends AbstractCollection<E>{
private static final String ERROR_MESSAGE = "error message";
private final int CAPACITY = 1000;
private final E[] data;
private int size;
private final MyIterator<E> myIter;
public MyCollection() {
this.size = 0;
data = (E[]) new Object[0];
myIter = new MyIterator<>(data, size);
}
public MyCollection(Collection<? extends E> incol) throws IllegalArgumentException{
if (incol.size() > CAPACITY) throw new IllegalArgumentException(ERROR_MESSAGE);
this.size = 0;
try {
data = (E[]) new Object[incol.size()];
} catch (ClassCastException ex) {
throw new IllegalArgumentException(ERROR_MESSAGE);
}
for (E el : incol) {
data[size++] = el;
}
myIter = new MyIterator<>(data, size);
}
public void print() {
for (E el : data) {
System.out.println(el);
}
}
@Override
public Iterator<E> iterator() {
return myIter;
}
@Override
public int size() {
return size;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(data);
result = prime * result + size;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyCollection<?> other = (MyCollection<?>) obj;
if (!Arrays.equals(data, other.data))
return false;
return size == other.size;
}
}