ΠΠ°Π»Ρ ΡΠΏΠΎΡΠ½ΠΎ Π²ΡΠ΄Π°Π΅Ρ ΡΡΠΎ ΠΎΠ±ΡΠ΅ΠΊΡΡ Π½Π΅ ΡΠ°Π²Π½Ρ, Ρ
ΠΎΡΡ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ΅ Π²ΡΠΎΠ΄Π΅ ΠΊΠ°ΠΊ Π² ΠΎΠ±ΠΎΠΈΡ
ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠ΅.
package com.javarush.task.task37.task3707;
import java.io.*;
import java.util.*;
public class AmigoSet<E> extends AbstractSet <E> implements Serializable, Cloneable, Set<E> {
private static final Object PRESENT = new Object();
private transient HashMap <E, Object> map;
public static void main(String[] args) throws IOException, ClassNotFoundException {
HashSet<String> hashSet = new HashSet<>();
hashSet.add("ddd");
hashSet.add("rrrr");
AmigoSet amigoSet = new AmigoSet(hashSet);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(amigoSet);
objectOutputStream.close();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
AmigoSet amigoSet1 = (AmigoSet) objectInputStream.readObject();
System.out.println(amigoSet.equals(amigoSet1));
System.out.println(amigoSet);
System.out.println("________");
System.out.println(amigoSet1);
}
public AmigoSet() {
this.map = new HashMap<>();
}
public AmigoSet(Collection<? extends E> collection) {
int capacity = Math.max(16, (int)Math.ceil(collection.size()/0.75f));
this.map = new HashMap<> (capacity);
collection.forEach(x -> map.put(x, PRESENT));
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
oos.writeObject(HashMapReflectionHelper.callHiddenMethod(this.map, "capacity"));
oos.writeObject(HashMapReflectionHelper.callHiddenMethod(this.map, "loadFactor"));
oos.writeObject(this.map.size());
for (E e: this.map.keySet()) {
oos.writeObject(e);
}
}
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
ois.defaultReadObject();
int capacity = (int) ois.readObject();
float loadFactor = (float) ois.readObject();
int size = (int) ois.readObject();
map = new HashMap<>(capacity, loadFactor);
for (int i =0; i<size; i++) {
E e = (E) ois.readObject();
map.put(e, PRESENT);
}
}
@Override
public int size() {
return this.map.size();
}
@Override
public boolean isEmpty() {
return this.map.isEmpty();
}
@Override
public boolean contains(Object o) {
return this.map.containsKey(o);
}
@Override
public Iterator iterator() {
return this.map.keySet().iterator();
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public Object[] toArray(Object[] a) {
return new Object[0];
}
@Override
public boolean add(E e) {
return this.map.put(e, PRESENT) == null;
}
@Override
public boolean remove(Object o) {
return this.map.remove(o) == null;
}
@Override
public boolean containsAll(Collection c) {
return false;
}
@Override
public boolean addAll(Collection c) {
return false;
}
@Override
public boolean retainAll(Collection c) {
return false;
}
@Override
public boolean removeAll(Collection c) {
return false;
}
@Override
public void clear() {
this.map.clear();
}
@Override
public Spliterator spliterator() {
return null;
}
@Override
public Object clone() {
try {
AmigoSet obj = (AmigoSet) super.clone();
obj.map = (HashMap) map.clone();
return obj;
} catch (Exception e) {
throw new InternalError();
}
}
}