diff --git a/bin/test/uebung/uebung09/collection/SetTest.class b/bin/test/uebung/uebung09/collection/SetTest.class new file mode 100644 index 0000000..1aa1f92 Binary files /dev/null and b/bin/test/uebung/uebung09/collection/SetTest.class differ diff --git a/bin/uebung09/collection/BigSet$1.class b/bin/uebung09/collection/BigSet$1.class new file mode 100644 index 0000000..5479c6d Binary files /dev/null and b/bin/uebung09/collection/BigSet$1.class differ diff --git a/bin/uebung09/collection/BigSet.class b/bin/uebung09/collection/BigSet.class new file mode 100644 index 0000000..9997eb4 Binary files /dev/null and b/bin/uebung09/collection/BigSet.class differ diff --git a/bin/uebung09/collection/EmptySet$1.class b/bin/uebung09/collection/EmptySet$1.class new file mode 100644 index 0000000..d2706cf Binary files /dev/null and b/bin/uebung09/collection/EmptySet$1.class differ diff --git a/bin/uebung09/collection/EmptySet.class b/bin/uebung09/collection/EmptySet.class new file mode 100644 index 0000000..73456b7 Binary files /dev/null and b/bin/uebung09/collection/EmptySet.class differ diff --git a/bin/uebung09/collection/SetFactory.class b/bin/uebung09/collection/SetFactory.class index 4914357..4762368 100644 Binary files a/bin/uebung09/collection/SetFactory.class and b/bin/uebung09/collection/SetFactory.class differ diff --git a/bin/uebung09/collection/SingeltonSet$1.class b/bin/uebung09/collection/SingeltonSet$1.class new file mode 100644 index 0000000..427fab0 Binary files /dev/null and b/bin/uebung09/collection/SingeltonSet$1.class differ diff --git a/bin/uebung09/collection/SingeltonSet.class b/bin/uebung09/collection/SingeltonSet.class new file mode 100644 index 0000000..8624d52 Binary files /dev/null and b/bin/uebung09/collection/SingeltonSet.class differ diff --git a/src/test/uebung/uebung09/collection/SetTest.java b/src/test/uebung/uebung09/collection/SetTest.java new file mode 100644 index 0000000..54a1547 --- /dev/null +++ b/src/test/uebung/uebung09/collection/SetTest.java @@ -0,0 +1,76 @@ +package test.uebung.uebung09.collection; + +import java.util.Iterator; + +import uebung09.collection.Set; +import uebung09.collection.SetFactory; +import java.util.NoSuchElementException; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SetTest { + + static String a = "test"; + static String b = "test2"; + static String c = "test3"; + + + public static void main(String[] args) { + } + + @Test + public void EmptySetTest(){ + + Set test = SetFactory.create(); + + Iterator i = test.iterator(); + + assertFalse(i.hasNext()); + assertThrows(NoSuchElementException.class, i::next); + assertFalse(test.contains(a)); + assertEquals(0,test.size()); + assertTrue(test.isEmpty()); + } + + @Test + public void SingeltonSetTest(){ + + Set test = SetFactory.create(a); + + Iterator i = test.iterator(); + + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertFalse(i.hasNext()); + assertThrows(NoSuchElementException.class, i::next); + assertTrue(test.contains(a)); + assertEquals(1,test.size()); + assertFalse(test.isEmpty()); + } + + @Test + public void BigSetTest() { + + Set test = SetFactory.create(a,b,c); + + Iterator i = test.iterator(); + + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertTrue(i.hasNext()); + assertTrue(a == i.next()); + assertFalse(i.hasNext()); + assertThrows(NoSuchElementException.class, i::next); + assertTrue(test.contains(a)); + assertEquals(1,test.size()); + assertFalse(test.isEmpty()); + } +} diff --git a/src/uebung09/collection/BigSet.java b/src/uebung09/collection/BigSet.java new file mode 100644 index 0000000..7de961b --- /dev/null +++ b/src/uebung09/collection/BigSet.java @@ -0,0 +1,94 @@ +package uebung09.collection; + +import java.util.Iterator; + +class BigSet implements Set{ + + private T item; + private Set rest; + + public BigSet(T item, Set rest){ + //TODO + this.item = item; + this.rest = rest; + } + + @Override + public Iterator iterator() { + return new Iterator() { + + private boolean returned = false; + + private Iterator restI = rest.iterator(); + + @Override + public boolean hasNext() { + return restI.hasNext(); + } + + @Override + public T next() { + if (!returned) { + returned = true; + return item; + } else { + return restI.next(); + } + } + }; + } + + @Override + public int size() { + int count = 1; + count += rest.size(); + + return count; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object el) { + if(el == item) { + return true; + } else { + return rest.contains(el); + } + } + + @Override + public Set union(Set other) { + if (other == EmptySet.getInstance()) { + return this; + } else if (other instanceof SingeltonSet && !contains(other.iterator().next())) { + return new BigSet(other.iterator().next(), this); + } else { + return new BigSet(item, rest.union(other)); + } + } + + @Override + public Set add(T element) { + if (contains(element)) { + return this; + } else { + return new BigSet(element, this); + } + } + + @Override + public Set intersection(Set other) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'intersection'"); + } + + @Override + public boolean subsetOf(Set other) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'subsetOf'"); + } +} diff --git a/src/uebung09/collection/EmptySet.java b/src/uebung09/collection/EmptySet.java new file mode 100644 index 0000000..55aa694 --- /dev/null +++ b/src/uebung09/collection/EmptySet.java @@ -0,0 +1,71 @@ +package uebung09.collection; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +class EmptySet implements Set { + + private static EmptySet INSTANCE; + + private EmptySet() {} + + @SuppressWarnings("unchecked") + public static EmptySet getInstance() { + if (INSTANCE == null) { + INSTANCE = new EmptySet<>(); + } + return (EmptySet) INSTANCE; + } + + @Override + public Iterator iterator() { + return new Iterator() { + + @Override + public boolean hasNext() { + return false; + } + + @Override + public T next() { + throw new NoSuchElementException(); + } + }; + } + + @Override + public int size() { + return 0; + } + + @Override + public boolean isEmpty() { + return true; + } + + @Override + public boolean contains(Object el) { + return false; + } + + @Override + public Set union(Set other) { + return other; + } + + @Override + public Set add(T element) { + return new SingeltonSet(element); + } + + @Override + public Set intersection(Set other) { + return other; + } + + @Override + public boolean subsetOf(Set other) { + return true; + } + +} diff --git a/src/uebung09/collection/Set.java b/src/uebung09/collection/Set.java index 00171e8..d982b02 100644 --- a/src/uebung09/collection/Set.java +++ b/src/uebung09/collection/Set.java @@ -38,10 +38,10 @@ public interface Set extends Iterable { /** * returns the set resulting from adding the specified element to this set. * - * @param element an element (must not be null) + * @param elem an element (must not be null) * @return the set resulting from adding the specified element to this set. */ - Set add(E element); + Set add(E elem); /** * Returns the intersection of this set and the specified set. diff --git a/src/uebung09/collection/SetFactory.java b/src/uebung09/collection/SetFactory.java index f8cfeb3..fb96697 100644 --- a/src/uebung09/collection/SetFactory.java +++ b/src/uebung09/collection/SetFactory.java @@ -10,9 +10,8 @@ public class SetFactory { * @param the element type of the returned set. * @return the empty set. */ - static Set create() { - // TODO implement - return null; + public static Set create() { + return EmptySet.getInstance(); } /** @@ -22,9 +21,8 @@ public class SetFactory { * @param the element type of the returned set. * @return the singleton set containing the specified element. */ - static Set create(T element) { - // TODO implement - return null; + public static Set create(T element) { + return new SingeltonSet(element); } /** @@ -34,8 +32,14 @@ public class SetFactory { * @param the element type of the returned set. * @return a set containing the specified elements. */ - static Set create(T... elems) { - // TODO implement - return null; + public static Set create(T... elems) { + // BigSet rtn = new BigSet(elems[1], new SingeltonSet<>(elems[1])); + Set set = EmptySet.getInstance(); + + for(T elem : elems) { + set = set.add(elem); + } + + return set; } } diff --git a/src/uebung09/collection/SingeltonSet.java b/src/uebung09/collection/SingeltonSet.java new file mode 100644 index 0000000..afa3870 --- /dev/null +++ b/src/uebung09/collection/SingeltonSet.java @@ -0,0 +1,77 @@ +package uebung09.collection; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +class SingeltonSet implements Set { + + private T item; + + public SingeltonSet(T item) { + this.item = item; + } + + @Override + public Iterator iterator() { + return new Iterator() { + + boolean returned = false; + + @Override + public boolean hasNext() { + return !returned; + } + + @Override + public T next() { + if (!returned) { + returned = true; + return item; + } else { + throw new NoSuchElementException(); + } + } + }; + } + + @Override + public int size() { + return 1; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(Object el) { + return el == item; + } + + @Override + public Set union(Set other) { + //TODO + return null; + } + + @Override + public Set add(T element) { + if (element == item) { + return this; + } else { + return new BigSet(item, new SingeltonSet(element)); + } + } + + @Override + public Set intersection(Set other) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'intersection'"); + } + + @Override + public boolean subsetOf(Set other) { + throw new UnsupportedOperationException(); + } +}