5 changed files with 0 additions and 349 deletions
@ -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