Compare commits
4 Commits
vollstaend
...
master
Author | SHA1 | Date |
---|---|---|
|
b018ca913c | 3 years ago |
|
0c982a51a9 | 3 years ago |
|
0e1c3a5dc1 | 3 years ago |
|
ae5d67017f | 3 years ago |
20 changed files with 5 additions and 972 deletions
@ -1,41 +0,0 @@ |
|||
package solution; |
|||
|
|||
import testat.SimpleActivity; |
|||
|
|||
public class SimpleActivityStack extends SimpleStack<SimpleActivity> { |
|||
|
|||
@Override |
|||
public int push(SimpleActivity activity) { |
|||
if (activity == null) { |
|||
return 0; |
|||
} |
|||
if (size() > 0) { |
|||
super.get(size() - 1).passivate(); |
|||
} |
|||
activity.activate(); |
|||
return super.push(activity); |
|||
} |
|||
|
|||
@Override |
|||
public SimpleActivity pop() { |
|||
SimpleActivity activity = super.pop(); |
|||
if (activity != null) { |
|||
activity.passivate(); |
|||
activity.destroy(); |
|||
if (size() > 0) { |
|||
super.get(size() - 1).activate(); |
|||
} |
|||
} |
|||
return activity; |
|||
} |
|||
|
|||
@Override |
|||
public SimpleActivity remove(int i) { |
|||
if (i == super.size() - 1) { |
|||
return this.pop(); |
|||
} |
|||
super.get(i).destroy(); |
|||
return super.remove(i); |
|||
} |
|||
|
|||
} |
@ -1,14 +0,0 @@ |
|||
package solution; |
|||
|
|||
import testat.ASimpleActivityStackTest; |
|||
import testat.ISimpleStack; |
|||
import testat.SimpleActivity; |
|||
|
|||
public class SimpleActivityStackTest extends ASimpleActivityStackTest { |
|||
|
|||
@Override |
|||
protected ISimpleStack<SimpleActivity> getInstance() { |
|||
return new SimpleActivityStack(); |
|||
} |
|||
|
|||
} |
@ -1,64 +0,0 @@ |
|||
package solution; |
|||
|
|||
import java.util.LinkedList; |
|||
import java.util.List; |
|||
|
|||
import testat.IAction; |
|||
import testat.ISimpleStack; |
|||
|
|||
public class SimpleStack<T> implements ISimpleStack<T> { |
|||
|
|||
private List<T> list; |
|||
|
|||
public SimpleStack() { |
|||
super(); |
|||
list = new LinkedList<T>(); |
|||
} |
|||
|
|||
@Override |
|||
public int push(T t) { |
|||
if (t != null) { |
|||
if (list.contains(t)) { |
|||
list.remove(t); |
|||
} |
|||
list.add(t); |
|||
return list.size(); |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
@Override |
|||
public T pop() { |
|||
if (list.size() == 0) { |
|||
return null; |
|||
} |
|||
return list.remove(list.size() - 1); |
|||
} |
|||
|
|||
@Override |
|||
public T remove(int i) { |
|||
return list.remove(i); |
|||
} |
|||
|
|||
@Override |
|||
public int size() { |
|||
return list.size(); |
|||
} |
|||
|
|||
@Override |
|||
public boolean contains(Object o) { |
|||
return list.contains(o); |
|||
} |
|||
|
|||
@Override |
|||
public T get(int i) { |
|||
return list.get(i); |
|||
} |
|||
|
|||
@Override |
|||
public void forAll(IAction<T> action) { |
|||
if (action != null) { |
|||
list.forEach(t -> action.execute(t)); |
|||
} |
|||
} |
|||
} |
@ -1,13 +0,0 @@ |
|||
package solution; |
|||
|
|||
import testat.ASimpleStackTest; |
|||
import testat.ISimpleStack; |
|||
|
|||
public class SimpleStackTest extends ASimpleStackTest { |
|||
|
|||
@Override |
|||
protected ISimpleStack<String> getInstance() { |
|||
return new SimpleStack<>(); |
|||
} |
|||
|
|||
} |
@ -1,66 +0,0 @@ |
|||
package testat; |
|||
|
|||
/** |
|||
* |
|||
* @author cm |
|||
* not provided |
|||
* |
|||
* @param <T> |
|||
*/ |
|||
public interface ISimpleStack<T> { |
|||
|
|||
/** |
|||
* Nimmt ein Objekt auf, das nicht null ist und noch nicht im Stack enthalten |
|||
* ist. Rückgabe: Anzahl an Elemente im Stack nach der Aufnahme oder 0, falls t |
|||
* == null ist das Objekt bereits enthalten, wird es ganz oben auf den Stapel |
|||
* gelegt. Hinweis: Verwenden Sie entsprechende Operationen der Collection/des |
|||
* Array und nicht pop/push |
|||
* |
|||
* @param t |
|||
* @return |
|||
*/ |
|||
public int push(T t); |
|||
|
|||
/** |
|||
* Entfernt das oberste Objekt und gibt es zurück. Liefert null, falls der Stack |
|||
* leer ist |
|||
*/ |
|||
public T pop(); |
|||
|
|||
/** |
|||
* gibt einen Verweise auf das i-te Objekt zurück. Das Objekt bleibt im //
|
|||
* Stack. Falls i nicht existiert, führt dies zur IndexOutOfBoundsException. //
|
|||
* Das unterste Element hat Index 0, das oberste Index size()-1 |
|||
* |
|||
* @param i index |
|||
* @return |
|||
*/ |
|||
public T get(int i); |
|||
|
|||
/** |
|||
* Wie get(i), nur wird das Objekt aus dem Stack entfernt |
|||
* |
|||
* @param i |
|||
* @return |
|||
*/ |
|||
public T remove(int i); |
|||
|
|||
/** |
|||
* Anzahl an Objekte im Stack |
|||
* |
|||
*/ |
|||
public int size(); |
|||
|
|||
/** |
|||
* true, falls das Objekt im Stack enthalten ist |
|||
* |
|||
*/ |
|||
public boolean contains(Object o); |
|||
|
|||
/** |
|||
* Wendet action.execute auf alle Objekte im Stack an |
|||
* |
|||
* @param action |
|||
*/ |
|||
public void forAll(IAction<T> action); |
|||
} |
@ -1,7 +1,10 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<classpath> |
|||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> |
|||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> |
|||
<attributes> |
|||
<attribute name="module" value="true"/> |
|||
</attributes> |
|||
</classpathentry> |
|||
<classpathentry kind="src" path="src"/> |
|||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/> |
|||
<classpathentry kind="output" path="bin"/> |
|||
</classpath> |
|||
|
@ -1,118 +0,0 @@ |
|||
package solution; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
import testat.ISimpleMap; |
|||
|
|||
public class SimpleArrayMap<K, V> implements ISimpleMap<K, V> { |
|||
|
|||
private K[] keys; |
|||
private V[] values; |
|||
private int size; |
|||
|
|||
public SimpleArrayMap(int arraySize) { |
|||
|
|||
this.keys = (K[]) new Object[arraySize]; |
|||
this.values = (V[]) new Object[arraySize]; |
|||
this.size = 0; |
|||
} |
|||
|
|||
@Override |
|||
public V put(K k, V v) { |
|||
|
|||
V tempValue; |
|||
|
|||
if (k == null || v == null) { |
|||
return null; |
|||
} |
|||
|
|||
for (int i = 0; i < values.length; i++) { |
|||
if (k.equals(keys[i])) { |
|||
tempValue = values[i]; |
|||
values[i] = v; |
|||
return tempValue; |
|||
} |
|||
} |
|||
|
|||
for (int i = 0; i < values.length; i++) { |
|||
if (keys[i] == null) { |
|||
keys[i] = k; |
|||
values[i] = v; |
|||
size++; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public V get(K k) { |
|||
|
|||
if (k == null) { |
|||
return null; |
|||
} |
|||
|
|||
for (int i = 0; i < values.length; i++) { |
|||
if (k.equals(keys[i])) { |
|||
return values[i]; |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public V remove(K k) { |
|||
|
|||
V tempValue; |
|||
|
|||
if (k == null) { |
|||
return null; |
|||
} |
|||
|
|||
for (int i = 0; i < values.length; i++) { |
|||
if (k.equals(keys[i])) { |
|||
tempValue = values[i]; |
|||
keys[i] = null; |
|||
values[i] = null; |
|||
size--; |
|||
return tempValue; |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public boolean contains(V v) { |
|||
|
|||
if (v == null) { |
|||
return false; |
|||
} |
|||
|
|||
for (int i = 0; i < values.length; i++) { |
|||
if (v.equals(values[i])) { |
|||
return true; |
|||
} |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public void forEach(Consumer<? super V> consumer) { |
|||
|
|||
for (int i = 0; i < values.length; i++) { |
|||
if (keys[i] != null) { |
|||
consumer.accept(values[i]); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public int size() { |
|||
return size; |
|||
} |
|||
|
|||
} |
@ -1,13 +0,0 @@ |
|||
package solution; |
|||
|
|||
import testat.ASimpleMapTest; |
|||
import testat.ISimpleMap; |
|||
|
|||
public class SimpleMapTest extends ASimpleMapTest{ |
|||
|
|||
@Override |
|||
protected ISimpleMap<String, String> getInstance() { |
|||
|
|||
return new SimpleArrayMap<>(5); |
|||
} |
|||
} |
@ -1,34 +0,0 @@ |
|||
package testat; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
public interface ISimpleMap<K, V> { |
|||
|
|||
// speichert zu dem Schlüssel k den Wert v, sofern beide nicht null sind
|
|||
// (ist mind. einer null, wird null zurück gegeben) .
|
|||
// Falls zu k bereits ein Wert w vorhanden ist, wird dieser durch v
|
|||
// ersetzt. Der alte Wert w wird zurückgegeben.
|
|||
// Falls zu k noch kein Wert gespeichert war, wird zu k der Wert v
|
|||
// aufgenommen und null zurückgegeben
|
|||
V put(K k, V v); |
|||
|
|||
// Liefert den zum Schlüssel k gespeicherten Wert. Liefert null,
|
|||
// falls k == null oder falls zu k kein Wert gespeichert ist
|
|||
V get(K k); |
|||
|
|||
// Entfernt den zum Schlüssel k gespeicherten Wert v und
|
|||
// liefert v zurück. Liefert null, falls k == null oder falls
|
|||
// zu k kein Wert gespeichert war
|
|||
V remove(K k); |
|||
|
|||
// Liefert true, falls der Wert v in der Map (zu irgendeinem Schlüssel)
|
|||
// gespeichert ist
|
|||
boolean contains(V v); |
|||
|
|||
// wendet die Methode consumer.accept(v) auf alle in der
|
|||
// Map gespeicherten Werte an
|
|||
void forEach(Consumer<? super V> consumer); |
|||
|
|||
// Anzahl an in der Map gespeicherten Werte
|
|||
int size(); |
|||
} |
@ -1,6 +0,0 @@ |
|||
package solution; |
|||
|
|||
public interface ICommand<E> { |
|||
|
|||
void execute(E e); |
|||
} |
@ -1,54 +0,0 @@ |
|||
package solution; |
|||
|
|||
import java.util.Collection; |
|||
import java.util.Comparator; |
|||
|
|||
public interface ISimpleList<E> { |
|||
|
|||
// Anzahl der Elemente in der Liste
|
|||
int size(); |
|||
|
|||
// fügt das Element e am Ende der Liste ein, sofern es ungleich null ist:
|
|||
// Liefert true, falls ein Element eingefügt wurde (false sonst)
|
|||
boolean add(E e); |
|||
|
|||
// fügt alle Elemente der Collection c ein (sofern diese nicht null sind).
|
|||
boolean add(Collection<E> c); |
|||
|
|||
// prüft, ob das Objekt o in der Liste enthalten ist (o == null führt zur
|
|||
// Rückgabe false)
|
|||
boolean contains(Object o); |
|||
|
|||
// entfernt das Objekt o aus der Liste // true: Ein Element wurde entfernt,
|
|||
// false sonst
|
|||
boolean remove(Object o); |
|||
|
|||
// Durchläuft alle Elemente der Liste und wendet command.execute auf jedes
|
|||
// Element an
|
|||
void forAll(ICommand<E> command); |
|||
|
|||
// fügt das Element an der Position i ein, sofern es nicht null ist und 0 <= i
|
|||
// <= Anzahl an Elementen gilt. Rückgabe true, false sonst.
|
|||
default void add(int i, E e) throws IndexOutOfBoundsException { |
|||
} |
|||
|
|||
// Liefert das Element, das an Position i steht, null falls i keinen gültigen
|
|||
// Index darstellt.
|
|||
default E get(int i) throws IndexOutOfBoundsException { |
|||
return null; |
|||
} |
|||
|
|||
// Ersetzt das i-te Element durch e, sofern es vorhanden ist (Index gültig)
|
|||
default void set(int i, E e) throws IndexOutOfBoundsException { |
|||
} |
|||
|
|||
// Löscht das i-te Element, sofern der Index gültig ist. Rückgabe true, false
|
|||
// sonst.
|
|||
default boolean remove(int i) { |
|||
return false; |
|||
} |
|||
|
|||
// Sortiermethode mittels Comparator
|
|||
default void sort(Comparator<E> comparator) { |
|||
} |
|||
} |
@ -1,36 +0,0 @@ |
|||
package solution; |
|||
|
|||
public class Node <E>{ |
|||
private Node<E> predecessor; // Vorgänger
|
|||
private Node<E> successor; // Nachfolger
|
|||
private E payload; // Nutzlast
|
|||
|
|||
//Konstruktor mit Parameter payload
|
|||
public Node(E payload) { |
|||
this.payload = payload; |
|||
} |
|||
|
|||
public Node<E> getPredecessor() { |
|||
return predecessor; |
|||
} |
|||
|
|||
public void setPredecessor(Node<E> predecessor) { |
|||
this.predecessor = predecessor; |
|||
} |
|||
|
|||
public Node<E> getSuccessor() { |
|||
return successor; |
|||
} |
|||
|
|||
public void setSuccessor(Node<E> e) { |
|||
this.successor = e; |
|||
} |
|||
|
|||
public E getPayload() { |
|||
return payload; |
|||
} |
|||
|
|||
public void setPayload(E payload) { |
|||
this.payload = payload; |
|||
} |
|||
} |
@ -1,152 +0,0 @@ |
|||
package solution; |
|||
|
|||
public class SimpleLinkedList<E> extends AbstractSimpleList<E> { |
|||
|
|||
Node<E> firstNode; |
|||
int size; |
|||
|
|||
public SimpleLinkedList() { |
|||
firstNode = null; |
|||
size = 0; |
|||
} |
|||
|
|||
@Override |
|||
public int size() { |
|||
return size; |
|||
} |
|||
|
|||
@Override |
|||
public boolean add(E e) { |
|||
if (e == null) { |
|||
return false; |
|||
} else if (firstNode == null) { |
|||
firstNode = new Node<E>(e); |
|||
} else { |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
if (currentNode.getSuccessor() == null) { |
|||
break; |
|||
} |
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
Node<E> newNode = new Node<E>(e); |
|||
currentNode.setSuccessor(newNode); |
|||
newNode.setPredecessor(currentNode); |
|||
} |
|||
|
|||
size++; |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public boolean remove(Object o) { |
|||
if (o == null) { |
|||
return false; |
|||
} |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
if (o.equals(currentNode.getPayload())) { |
|||
currentNode.setPayload(null); |
|||
size--; |
|||
return true; |
|||
} |
|||
|
|||
if (currentNode.getSuccessor() == null) { |
|||
break; |
|||
} |
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public boolean contains(Object o) { |
|||
if (o == null) { |
|||
return false; |
|||
} |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
if (o.equals(currentNode.getPayload())) { |
|||
return true; |
|||
} |
|||
|
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public void forAll(ICommand<E> command) { |
|||
if (command != null) { |
|||
Node<E> currentNode = firstNode; |
|||
while (currentNode != null) { |
|||
command.execute(currentNode.getPayload()); |
|||
currentNode = currentNode.getSuccessor(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private Node<E> getIndex(int i) { |
|||
|
|||
if (i < 0 || size <= i) { |
|||
return null; |
|||
} |
|||
Node<E> currentNode = firstNode; |
|||
int currentIndex = 0; |
|||
while (currentNode != null) { |
|||
if (currentIndex >= i) { |
|||
|
|||
if (currentNode.getPayload() != null) { |
|||
return currentNode; |
|||
} |
|||
} |
|||
currentNode = currentNode.getSuccessor(); |
|||
currentIndex++; |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public E get(int i) throws IndexOutOfBoundsException { |
|||
|
|||
return getIndex(i).getPayload(); |
|||
} |
|||
|
|||
public void set(int i, E e) throws IndexOutOfBoundsException { |
|||
|
|||
getIndex(i).setPayload(e); |
|||
} |
|||
|
|||
public boolean remove(int i) throws IndexOutOfBoundsException { |
|||
|
|||
if (getIndex(i) != null) { |
|||
getIndex(i).setPayload(null); |
|||
size--; |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
public void add(int i, E e) throws IndexOutOfBoundsException { |
|||
if (size == 0) { |
|||
if (i != 0) { |
|||
throw new IndexOutOfBoundsException("Index " + i + " is out of bounds!"); |
|||
} |
|||
set(0, e); |
|||
|
|||
} else if (i == size) { |
|||
add(e); |
|||
|
|||
} else if (size > 0 && i < size) { |
|||
Node<E> newNode = new Node<>(e);// node3
|
|||
|
|||
Node<E> tempNode = getIndex(i); // node2
|
|||
Node<E> preTempNode = getIndex(i).getPredecessor(); // node1
|
|||
|
|||
preTempNode.setSuccessor(newNode); |
|||
newNode.setPredecessor(preTempNode); |
|||
newNode.setSuccessor(tempNode); |
|||
tempNode.setPredecessor(newNode); |
|||
} |
|||
} |
|||
} |
@ -1,9 +0,0 @@ |
|||
package solution; |
|||
|
|||
public class SimpleLinkedListTest extends SimpleListTest90{ |
|||
|
|||
@Override |
|||
protected ISimpleList<String> getInstance1() { |
|||
return new SimpleLinkedList<>(); |
|||
} |
|||
} |
@ -1,123 +0,0 @@ |
|||
package solution_g11; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
import testat_g11.IPDetermination; |
|||
import testat_g11.IPriorityQueue; |
|||
import testat_g11.Ticket; |
|||
import testat_g11.TicketStatus; |
|||
|
|||
public class SimplePriorityQueue00000<T extends Ticket> implements IPriorityQueue<T> { |
|||
|
|||
private IPDetermination<Ticket> prioDet; |
|||
private int size; |
|||
private T[] tickets; |
|||
|
|||
public SimplePriorityQueue00000(IPDetermination<Ticket> prioDet) { |
|||
super(); |
|||
this.prioDet = prioDet; |
|||
|
|||
tickets = (T[]) java.lang.reflect.Array.newInstance(Ticket.class, 10); |
|||
size = 0; |
|||
} |
|||
|
|||
@Override |
|||
public boolean add(T t) { |
|||
if (t == null) { |
|||
return false; |
|||
} |
|||
if (t.getPriority() < 0) { |
|||
return false; |
|||
} |
|||
int priority = setPriority(t, t.getPriority()); |
|||
if (priority >= 0) { |
|||
return true; // fehler
|
|||
} else { |
|||
tickets[size] = t; |
|||
size++; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public T poll() { |
|||
if (size == 0) { |
|||
return null; |
|||
} |
|||
T ticket = tickets[0]; |
|||
for (int i = 1; i < size; i++) { |
|||
if (prioDet.calculate(tickets[i]) > prioDet.calculate(ticket)) { |
|||
ticket.incrementAge(); |
|||
ticket = tickets[i]; |
|||
} else { |
|||
tickets[i].incrementAge(); |
|||
} |
|||
} |
|||
remove(ticket); |
|||
return ticket; |
|||
} |
|||
|
|||
@Override |
|||
public boolean contains(T t) { |
|||
int index = getIndexOfTicket(t); |
|||
return index >= 0; |
|||
} |
|||
|
|||
private int getIndexOfTicket(T t) { |
|||
for (int i = 0; i < size; i++) { |
|||
if (tickets[i].equals(t)) { |
|||
return i; |
|||
} |
|||
|
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
@Override |
|||
public int getTotalPriority(T t) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index >= 0) { |
|||
return prioDet.calculate(tickets[index]); |
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
@Override |
|||
public int setPriority(T t, int newPriority) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index >= 0) { |
|||
tickets[index].setPriority(newPriority); |
|||
return prioDet.calculate(tickets[index]); |
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
@Override |
|||
public int size() { |
|||
return size; |
|||
} |
|||
|
|||
@Override |
|||
public void forAll(Consumer<T> action) { |
|||
for (int i = 0; i < size; i++) { |
|||
action.accept(tickets[i]); |
|||
} |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public boolean remove(T t) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index < 0) { |
|||
return false; |
|||
} |
|||
t.setStatus(TicketStatus.FINISHED); |
|||
for (int i = index; i < size - 1; i++) { |
|||
tickets[i] = tickets[i + 1]; |
|||
} |
|||
tickets[size - 1] = null; |
|||
size = size - 1; |
|||
return true; |
|||
} |
|||
|
|||
} |
@ -1,45 +0,0 @@ |
|||
package solution_g11; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
import testat_g11.ASimplePriorityQueueBaseTest; |
|||
import testat_g11.IPDetermination; |
|||
import testat_g11.IPriorityQueue; |
|||
import testat_g11.Ticket; |
|||
|
|||
public class SimplePriorityQueueTest00000 extends ASimplePriorityQueueBaseTest { |
|||
|
|||
protected static IPDetermination<Ticket> prioDetermination = new IPDetermination<>() { |
|||
|
|||
@Override |
|||
public int calculate(Ticket t) { |
|||
// TODO Auto-generated method stub
|
|||
return IPDetermination.super.calculate(t); |
|||
} |
|||
|
|||
}; |
|||
|
|||
@Override |
|||
protected IPriorityQueue<Ticket> getInstance() { |
|||
// TODO Auto-generated method stub
|
|||
return new SimplePriorityQueue00000<Ticket>(new IPDetermination<Ticket>() { |
|||
}); |
|||
} |
|||
|
|||
@Test |
|||
public void testPoll5() { |
|||
queue.add(t1); |
|||
queue.add(t2); |
|||
queue.add(t3); |
|||
queue.poll(); |
|||
queue.poll(); |
|||
queue.add(t8); |
|||
Ticket actual = queue.poll(); |
|||
assertEquals(t2, actual); |
|||
actual = queue.poll(); |
|||
assertEquals(t8, actual); |
|||
} |
|||
|
|||
} |
@ -1,29 +0,0 @@ |
|||
package testat_g11; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
public interface IPriorityQueue<T extends Ticket> { |
|||
|
|||
// Nimmt t in die Queue auf, sofern t != null und t noch nicht enthalten und die
|
|||
// Priorität >= 0 ist.
|
|||
// ist t bereits enthalten, wird es nicht erneut aufgenommen, aber die Priorität
|
|||
// des bestehenden Tickets durch die Priorität von t ersetzt.
|
|||
// Rückgabe: False, wenn t == null, true sonst
|
|||
boolean add(T t); |
|||
|
|||
// Entfernt das Element mit der größten Priorität aus der Queue
|
|||
T poll(); |
|||
|
|||
boolean contains(T t); |
|||
|
|||
boolean remove(T t); |
|||
|
|||
int getTotalPriority(T t); |
|||
|
|||
int setPriority(T t, int newPriority); |
|||
|
|||
int size(); |
|||
|
|||
void forAll(Consumer<T> action); |
|||
|
|||
} |
@ -1,12 +0,0 @@ |
|||
package testat_g11; |
|||
|
|||
public class SimplePriorityQeueTest11111 extends ASimplePriorityQueueTest{ |
|||
|
|||
@Override |
|||
protected IPriorityQueue<Ticket> getInstance() { |
|||
// TODO Auto-generated method stub
|
|||
return new SimplePriorityQueue<Ticket>(new IPDetermination<Ticket>() { |
|||
}); |
|||
} |
|||
|
|||
} |
@ -1,140 +0,0 @@ |
|||
package testat_g11; |
|||
|
|||
import java.util.function.Consumer; |
|||
|
|||
public class SimplePriorityQueue<T extends Ticket> implements IPriorityQueue<T> { |
|||
|
|||
private T[] tickets; |
|||
private int size; |
|||
private IPDetermination<T> prioDet; |
|||
|
|||
public SimplePriorityQueue(IPDetermination<T> prioDet) { |
|||
super(); |
|||
this.prioDet = prioDet; |
|||
tickets = (T[]) java.lang.reflect.Array.newInstance(Ticket.class, 10); |
|||
size = 0; |
|||
} |
|||
|
|||
private int getIndexOfTicket(T t) { |
|||
for (int i = 0; i < size; i++) { |
|||
if (tickets[i].equals(t)) { |
|||
return i; |
|||
} |
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
// Nimmt t in die Queue auf, sofern t != null und t noch nicht enthalten und
|
|||
// die Priorität >= 0 ist. Ist t bereits enthalten, wird es nicht erneut
|
|||
// aufgenommen, aber die Priorität des bestehenden Tickets durch die Priorität
|
|||
// von t ersetzt. Rückgabe: False, wenn t == null, true sonst
|
|||
@Override |
|||
public boolean add(T t) { |
|||
if (t == null | t.getPriority() < 0) { |
|||
return false; |
|||
} else if (this.contains(t)) { |
|||
setPriority(tickets[getIndexOfTicket(t)], t.getPriority()); |
|||
} else { |
|||
tickets[size] = t; |
|||
size++; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
|
|||
// Entnimmt das Ticket mit der größten Gesamtpriorität (gemäß Implementierung
|
|||
// von IPDetermination. Falls zwei Tickets die gleiche Gesamtpriorität
|
|||
// aufweisen, wird das Ticket gewählt, dessen BeginOfTicket-Datum kleiner ist
|
|||
// (das ältere Ticket). Bei den restlichen Tickets wird das Alter mit der
|
|||
// Methode incrementAge() erhöht (Siehe Ticket im Paket testat_g11)
|
|||
@Override |
|||
public T poll() { |
|||
if (size == 0) { |
|||
return null; |
|||
} |
|||
T tick = tickets[0]; |
|||
|
|||
for (int i = 1; i < size; i++) { |
|||
if (prioDet.calculate(tickets[i]) > prioDet.calculate(tick)) { |
|||
tick.incrementAge(); |
|||
tick = tickets[i]; |
|||
} else { |
|||
tickets[i].incrementAge(); |
|||
} |
|||
} |
|||
|
|||
// Der Fall 2 Tickets haben die gleiche HöchstPrio
|
|||
for (int i = 0; i < size; i++) { |
|||
if (prioDet.calculate(tickets[i]) == prioDet.calculate(tick) & !tickets[i].equals(tick)) { |
|||
if (tick.getBeginOfTicket() != null && tickets[i].getBeginOfTicket() != null) { |
|||
if (tick.getBeginOfTicket().after(tickets[i].getBeginOfTicket())) { |
|||
tick = tickets[i]; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
remove(tick); |
|||
return tick; |
|||
} |
|||
|
|||
// True: Ticket t ist enthalten, false sonst
|
|||
@Override |
|||
public boolean contains(T t) { |
|||
return this.getIndexOfTicket(t) >= 0; |
|||
} |
|||
|
|||
// True: Ticket t wurde entfernt, false sonst. Der Status des entfernten
|
|||
// Tickets wird auf TicketStatus.FINISHED gesetzt
|
|||
@Override |
|||
public boolean remove(T t) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index < 0) { |
|||
return false; |
|||
} |
|||
t.setStatus(TicketStatus.FINISHED); |
|||
for (int i = index; i < size - 1; i++) { |
|||
tickets[i] = tickets[i + 1]; |
|||
} |
|||
tickets[size - 1] = null; |
|||
size--; |
|||
return true; |
|||
} |
|||
|
|||
// Liefert die Gesamtpriorität (IPDetermination) von t bzw. -1 falls t nicht
|
|||
// enthalten ist
|
|||
@Override |
|||
public int getTotalPriority(T t) { |
|||
if (!this.contains(t)) { |
|||
return -1; |
|||
} |
|||
return prioDet.calculate(t); |
|||
} |
|||
|
|||
// Setzt die Priorität von t auf newPriority. Rückgabe wie getPriority()
|
|||
@Override |
|||
public int setPriority(T t, int newPriority) { |
|||
int index = getIndexOfTicket(t); |
|||
if (index >= 0) { |
|||
tickets[index].setPriority(newPriority); |
|||
return prioDet.calculate(tickets[index]); |
|||
} |
|||
return -1; |
|||
} |
|||
|
|||
// Anzahl der Elemente in der Queue
|
|||
@Override |
|||
public int size() { |
|||
return size; |
|||
} |
|||
|
|||
// Wendet die Methode action.accept(…) auf alle Tickets an, die in der Queue
|
|||
// enthalten sind
|
|||
@Override |
|||
public void forAll(Consumer<T> action) { |
|||
for (int i = 0; i < size; i++) { |
|||
action.accept(tickets[i]); |
|||
} |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue