Skip to content

Instantly share code, notes, and snippets.

@thegabriele97
Created April 14, 2018 08:10
Show Gist options
  • Save thegabriele97/7b813af3f8281a4e7639a0aa37b89990 to your computer and use it in GitHub Desktop.
Save thegabriele97/7b813af3f8281a4e7639a0aa37b89990 to your computer and use it in GitHub Desktop.
package hydraulic;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import hydraulic.exceptions.*;
/**
* Main class that act as a container of the elements for
* the simulation of an hydraulics system
*
*/
public class HSystem {
private final static byte DEFAULT_INITIAL_NUMBER_OF_ELEMENTS = 0x3;
private Source source;
private List<Element> elements;
/**
* constructor of this class
*/
public HSystem() {
source = null;
elements = new ArrayList<>(DEFAULT_INITIAL_NUMBER_OF_ELEMENTS);
}
/**
* Adds a new element to the system
* @param elem
* @throws MoreThanASourceException if there is already a Source Element
* in the system afert a Source Element is passed as argument
*/
public void addElement(Element elem) {
if (elem instanceof Source) {
if (source != null) {
throw new MoreThanASourceException();
}
source = (Source)elem;
}
elements.add(elem);
}
/**
* returns the element added so far to the system
* @return an array of elements whose length is equal to the number of added elements
*/
public Element[] getElements() {
Element[] elements = new Element[this.elements.size()];
int cursor = 0;
for (Element e : this.elements) {
elements[cursor++] = e;
}
return elements;
}
/**
* Prints the layout of the system starting at each Source
*/
public String layout() {
StringBuilder string = new StringBuilder();
getLayoutRecursively(source, source, 0, string);
return string.toString();
}
/**
* starts the simulation of the system
*
* @throws NoSourceException if there is no Source in the System.
* @throws NoSinkFoundAtEndOfPathException if there is no Sink at end of a Path
*/
public void simulate() {
Stack<Element> splitStack = new Stack<>();
Element currentElement = source;
while (true) {
System.out.println(currentElement);
if (currentElement instanceof Sink) {
if (splitStack.size() <= 0) {
break;
}
currentElement = splitStack.pop();
continue;
}
if (currentElement instanceof Split) {
Element[] elementsInTheSplit = ((Split)currentElement).getOutputs();
splitStack.add(elementsInTheSplit[1]);
currentElement = elementsInTheSplit[0];
continue;
}
currentElement = currentElement.getOutput();
}
}
private void getLayoutRecursively(Element currentElement, Element prev, int prevLen, StringBuilder layoutBuffer) {
if (currentElement != source) {
layoutBuffer.append(' ');
if (prev instanceof Split) {
layoutBuffer.append('+');
}
layoutBuffer.append("-> ");
}
layoutBuffer.append(currentElement);
if (currentElement instanceof Split) {
Element[] elements = ((Split)currentElement).getOutputs();
prevLen += currentElement.toString().length() + 4;
getLayoutRecursively(elements[0], currentElement, prevLen, layoutBuffer);
layoutBuffer.append('\n');
for (int i = 0; i <= prevLen; i++) {
layoutBuffer.append(' ');
}
layoutBuffer.append("|\n");
for (int i = 0; i < prevLen; i++) {
layoutBuffer.append(' ');
}
getLayoutRecursively(elements[1], currentElement, prevLen + 3, layoutBuffer);
} else {
try {
getLayoutRecursively(currentElement.getOutput(), currentElement, prevLen + currentElement.toString().length() + 2, layoutBuffer);
} catch (UnsupportedOperationException e) {
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment