Compare commits

..

10 commits

6 changed files with 503 additions and 6 deletions

View file

@ -7,17 +7,21 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BracketServer implements CommandExecutor {
private static Pattern bracketPattern = Pattern.compile("(/\\*|\\*/|[(){}\\[\\]])");
private static Pattern bracketPattern = Pattern.compile("(/\\*|\\*/|[(){}\\[\\]<>])");
private static Map<String, String> bracketPairs;
public BracketServer() {
super();
bracketPairs = new HashMap<>();
bracketPairs.put("(", ")");
bracketPairs.put("[", "]");
bracketPairs.put("{", "}");
bracketPairs.put("/*", "*/");
bracketPairs = new HashMap<>() {
{
put("(", ")");
put("[", "]");
put("{", "}");
put("<", ">");
put("/*", "*/");
}
};
}
@Override

View file

@ -0,0 +1,261 @@
package ch.nuth.zhaw.exbox;
import java.util.AbstractList;
/**
* Provides the functionality to store a collection of items.
*/
public class MyList extends AbstractList<Object> {
/**
* Represents the node of a list.
*/
protected static interface IListNode {
/**
* Gets the next node neighbouring this node.
*
* @return
* The next node neighbouring this node.
*/
IListNode getNextNode();
/**
* Sets the next node neighbouring this node.
*
* @param nextNode
* The value to set.
*/
void setNextNode(IListNode nextNode);
/**
* Gets the previous node neighbouring this node.
*
* @return
* The previous node neighbouring this node.
*/
IListNode getPreviousNode();
/**
* Sets the previous node neighbouring this node.
*
* @param previousNode
* The value to set.
*/
void setPreviousNode(IListNode previousNode);
/**
* Gets the item contained by this node.
*
* @return
* The item contained by this node.
*/
Object getItem();
/**
* Sets the item contained by this node.
*
* @param item
* The value to set.
*/
void setItem(Object item);
}
/**
* Represents the node of a list.
*/
protected static class ListNode implements IListNode {
/**
* The next node neighbouring this node.
*/
private IListNode nextNode;
/**
* The previous node neighbouring this node.
*/
private IListNode previousNode;
/**
* The item contained by this node.
*/
private Object item;
/**
* Initializes a new instance of the {@linkplain ListNode `ListNode`} class.
*/
public ListNode() {
this(null);
}
/**
* Initializes a new instance of the {@linkplain ListNode `ListNode`} class.
*
* @param item
* The item contained by this node.
*/
public ListNode(Object item) {
this.item = item;
nextNode = this;
previousNode = this;
}
/**
* Initializes a new instance of the {@linkplain ListNode `ListNode`} class.
*
* @param nextNode
* The next node neighbouring this node.
*
* @param previousNode
* The previous node neighbouring this node.
*/
public ListNode(Object item, IListNode nextNode, IListNode previousNode) {
this(item);
this.nextNode = nextNode;
this.previousNode = previousNode;
}
/**
* Gets the next node neighbouring this node.
*
* @return
* The next node neighbouring this node.
*/
public IListNode getNextNode() {
return nextNode;
}
/**
* Sets the next node neighbouring this node.
*
* @param nextNode
* The value to set.
*/
public void setNextNode(IListNode nextNode) {
this.nextNode = nextNode;
}
/**
* Gets the previous node neighbouring this node.
*
* @return
* The previous node neighbouring this node.
*/
public IListNode getPreviousNode() {
return previousNode;
}
/**
* Sets the previous node neighbouring this node.
*
* @param previousNode
* The value to set.
*/
public void setPreviousNode(IListNode previousNode) {
this.previousNode = previousNode;
}
/**
* Gets the item contained by this node.
*
* @return
* The item contained by this node.
*/
public Object getItem() {
return item;
}
/**
* Sets the item contained by this node.
*
* @param item
* The value to set.
*/
public void setItem(Object item) {
this.item = item;
}
}
/**
* The anchor of the list.
*/
private IListNode anchor = new ListNode();
/**
* Gets the anchor of the list.
*
* @return
* The anchor of the list.
*/
protected IListNode getAnchor() {
return anchor;
}
/**
* Inserts the specified `item` after the specified `node`.
*
* @param node
* The node to insert the `item` after.
*
* @param item
* The item to insert.
*/
protected void insertAfter(IListNode node, Object item) {
IListNode newNode = new ListNode(item, node.getNextNode(), node);
node.setNextNode(newNode);
newNode.getNextNode().setPreviousNode(newNode);
}
@Override
public boolean add(Object e) {
insertAfter(getAnchor().getPreviousNode(), e);
return true;
}
@Override
public boolean remove(Object o) {
for (IListNode node = getAnchor().getNextNode(); node != getAnchor(); node = node.getNextNode()) {
if (node.getItem().equals(o)) {
node.getPreviousNode().setNextNode(node.getNextNode());
node.getNextNode().setPreviousNode(node.getPreviousNode());
return true;
}
}
return false;
}
@Override
public Object get(int index) {
IListNode currentNode = getAnchor().getNextNode();
for (int i = 0; i < index; i++) {
currentNode = currentNode.getNextNode();
if (currentNode == getAnchor()) {
throw new IndexOutOfBoundsException();
}
}
return currentNode.getItem();
}
@Override
public boolean isEmpty() {
return getAnchor().getNextNode() == getAnchor();
}
@Override
public int size() {
int result = 0;
for (IListNode node = getAnchor().getNextNode(); node != getAnchor(); node = node.getNextNode()) {
result++;
}
return result;
}
@Override
public void clear() {
getAnchor().setNextNode(getAnchor());
getAnchor().setPreviousNode(getAnchor());
}
}

View file

@ -0,0 +1,46 @@
package ch.nuth.zhaw.exbox;
/**
* Represents a list with the ability to sort elements.
*/
public class MySortedList extends MyList {
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean add(Object item) {
if (isEmpty()) {
return super.add(item);
} else {
IListNode currentNode = getAnchor().getNextNode();
while (true) {
int comparisonResult;
try {
if (item instanceof Comparable x &&
currentNode.getItem() instanceof Comparable y) {
comparisonResult = x.compareTo(y);
} else {
throw new UnsupportedOperationException();
}
} catch (Exception e) {
comparisonResult = 0;
}
if (comparisonResult == 0) {
insertAfter(currentNode, item);
return true;
} else if (comparisonResult < 0) {
insertAfter(currentNode.getPreviousNode(), item);
return true;
} else {
currentNode = currentNode.getNextNode();
if (currentNode == getAnchor()) {
insertAfter(currentNode.getPreviousNode(), item);
return true;
}
}
}
}
}
}

View file

@ -25,5 +25,12 @@ public class ADS1_3_test {
test("[(])", false);
test("[(3 +3)* 35 +3]* {3 +2}", true);
test("[({3 +3)* 35} +3]* {3 +2}", false);
test("(", false);
test(")", false);
test("<(<>)>", true);
test("<(<)>>", false);
test("/* hallo */", true);
test("/*/* */", false);
test("/*", false);
}
}

View file

@ -0,0 +1,99 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.LinkedList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @(#)ListTest.java
*
*
* @author
* @version 1.00 2017/8/30
*/
public class ADS2_3_test {
MyList list;
@BeforeEach
public void setUp() {
list = new MyList();
}
@Test
public void testAdd() {
list.clear();
list.add("A");
assertEquals("A", list.get(0));
}
@Test
public void testAdd2() {
list.clear();
list.add("A");
list.add("B");
assertEquals("A", list.get(0));
assertEquals("B", list.get(1));
}
@Test
public void testAdd3() {
list.clear();
list.add("A");
list.add("B");
list.add("C");
assertEquals("A", list.get(0));
assertEquals("B", list.get(1));
assertEquals("C", list.get(2));
}
@Test
public void testSize() {
list.clear();
assertEquals(0, list.size());
testAdd2();
assertEquals(2, list.size());
}
@Test
public void testRemove() {
list.clear();
list.add("A");
list.remove("A");
assertEquals(0, list.size());
list.add("A");
list.remove("B");
assertEquals(1, list.size());
list.remove("A");
assertEquals(0, list.size());
}
@Test
public void testMixed() {
list.clear();
List<Character> list2 = new LinkedList<>();
for (int i = 0; i < 100; i++) {
Character c = (char) ('A' + (Math.random() * 26));
int op = (int) (Math.random() * 2);
switch (op) {
case 0:
list.add(c);
list2.add(c);
break;
case 1:
list.remove(c);
list2.remove(c);
break;
}
}
assertEquals(list2.size(), list.size());
for (int i = 0; i < list.size(); i++) {
char c1 = (char) list.get(i);
char c2 = (char) list2.get(i);
assertEquals(c1, c2);
}
}
}

View file

@ -0,0 +1,80 @@
package ch.nuth.zhaw.exbox;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @(#)ListTest.java
*
*
* @author
* @version 1.00 2017/8/30
*/
public class ADS2_4_test {
MySortedList list;
@BeforeEach
public void setUp() {
list = new MySortedList();
}
@Test
public void testAdd() {
list.clear();
list.add("A");
Object o = list.get(0);
assertEquals("A", o);
}
@Test
public void testAdd2() {
list.clear();
list.add("B");
list.add("A");
assertEquals("A", list.get(0));
assertEquals("B", list.get(1));
}
@Test
public void testAdd3() {
list.clear();
list.add("C");
list.add("B");
list.add("A");
assertEquals("A", list.get(0));
assertEquals("B", list.get(1));
assertEquals("C", list.get(2));
}
@Test
public void testMixed() {
List<Character> list2 = new LinkedList<>();
for (int i = 0; i < 100; i++) {
Character c = (char) ('A' + (Math.random()*26));
int op = (int)(Math.random()*2);
switch (op) {
case 0:
list.add(c);
list2.add(c);
break;
case 1:
list.remove(c);
list2.remove(c);
break;
}
}
Collections.sort(list2);
assertEquals(list2.size(), list.size());
for (int i = 0; i < list.size(); i++) {
char c1 = (char)list.get(i);
char c2 = (char)list2.get(i);
assertEquals(c1, c2);
}
}
}