How can I restrict the generation of permutations? (In Java) - java

Dataset:
P1: Lion, Snow, Chair
P2: Min: 0, Max: 28
P3: Min: 34, Max is 39.
My Program is fed the above dataset (P1, P2, P3) as a series of arraylists. From this it continuously outputs different variations of a sequence including one element from each part (P1, P2, P3), until all possible permutations have been generated. (When generated P2 and P3 can be any number between their respective Min and Max.)
Examples of these sequences:
[Lion, 2, 37]
[Lion, 3, 34]
[Lion, 3, 35]
[Chair, 15, 35]
[Chair, 15, 36]
[Chair, 15, 37]
[Snow, 25, 36]
[Snow, 25, 37]
[Snow, 26, 34]
How?
To achieve this, I make use of the getCombinations function with P1,
P2 and P3 as parameters. To prepare the P2 and P3 arraylists for
use, I make use of the fillArrayList function which iterates from a
min to a max filling and then returning the relevant arraylist.
The problem I am facing is, I'm confused (lost) as to how I restrict the output of permutations which can lead to a 'Bad outcome' as below:
e.g.
P1 = Lion && P2 > 23 && P3 <= 35 Then Bad Outcome.
P1 = Lion && P2 < 13 && P3 >= 37 Then Bad Outcome.
P1 = Chair && P2 < 7 && P3 = 34 Then Bad Outcome.
Although I would be content to statically encoding a series of conditional statements for each, as these steps are read from a file which can change, this approach isn't applicable.
Code:
static ArrayList<ArrayList<String>> dataset = new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
static ArrayList<String> NegativePredictions = new ArrayList<String>();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
init();
for (ArrayList<String> curArrayList : dataset) {
ArrayList<String> currentRule = new ArrayList<String>();
if (curArrayList.size() > 2) {
currentRule = curArrayList;
} else {
currentRule = new ArrayList<String>(
fillArrayList(Integer.parseInt(curArrayList.get(0)), Integer.parseInt(curArrayList.get(1))));
}
rows.add(currentRule);
}
getCombinations(rows).forEach(System.out::println);
}
public static void init() throws IOException {
ArrayList<String> P1 = new ArrayList<String>(Arrays.asList("Lion", "Snow", "Chair"));
ArrayList<String> P2 = new ArrayList<String>(Arrays.asList("0", "28"));
ArrayList<String> P3 = new ArrayList<String>(Arrays.asList("34", "37"));
dataset = new ArrayList<ArrayList<String>>(Arrays.asList(P1, P2, P3));
NegativePredictions = new ArrayList<String>(Files.readAllLines(Paths.get("Predict.txt")));
}
public static ArrayList<String> fillArrayList(Integer start, Integer end) {
ArrayList<String> returnedList = new ArrayList<String>();
for (int i = start; i <= end; i++) {
returnedList.add(String.valueOf(i));
}
return returnedList;
}
#SuppressWarnings("unchecked")
public static <T> List<List<T>> getCombinations(Collection<? extends Iterable<T>> valueSetCollection) {
Iterable<T>[] valueSets = new Iterable[valueSetCollection.size()];
Iterator<T>[] valueIters = new Iterator[valueSetCollection.size()];
T[] values = (T[]) new Object[valueSetCollection.size()];
int i = 0;
for (Iterable<T> valueSet : valueSetCollection) {
valueSets[i] = valueSet; // Copy to array for fast index lookup
valueIters[i] = valueSet.iterator();
values[i] = valueIters[i].next(); // Fail if a wordSet is empty
i++;
}
List<List<T>> combinations = new ArrayList<>();
NEXT_COMBO: for (;;) {
combinations.add(Arrays.asList(values.clone()));
for (i = values.length - 1; i >= 0; i--) {
if (valueIters[i].hasNext()) {
values[i] = valueIters[i].next();
continue NEXT_COMBO;
}
valueIters[i] = valueSets[i].iterator();
values[i] = valueIters[i].next();
}
return combinations;
}
}
}
What would you recommend?

Consider encoding a rule, where a set of rules determines if that particular candidate permutation is included or excluded. For example, an interface to define a rule:
public interface ExclusionRule<X> {
public boolean isExcluded(X x);
}
along with several implementations which know how to make comparisons with a String or an Integer:
public class ExcludeIfEqualStringRule implements ExclusionRule<String> {
private final String exclusion;
public ExcludeIfEqualStringRule(String exclusion) {
this.exclusion = exclusion;
}
#Override
public boolean isExcluded(String x) {
return x.equals(exclusion);
}
}
public abstract class AbstractExcludeIntegerRule implements ExclusionRule<Integer> {
private final int threshold;
private final ExclusionRule<Integer> or;
public AbstractExcludeIntegerRule(int threshold, ExclusionRule<Integer> or) {
this.threshold = threshold;
this.or = or;
}
#Override
public final boolean isExcluded(Integer x) {
if (or != null) {
return or.isExcluded(x) || doComparison(x, threshold);
}
return doComparison(x, threshold);
}
protected abstract boolean doComparison(int x, int threshold);
}
public class ExcludeIfGreaterThanIntegerRule extends AbstractExcludeIntegerRule {
public ExcludeIfGreaterThanIntegerRule(int threshold, ExclusionRule<Integer> or) {
super(threshold, or);
}
public ExcludeIfGreaterThanIntegerRule(int threshold) {
this(threshold, null);
}
#Override
protected boolean doComparison(int x, int threshold) {
return x > threshold;
}
}
public class ExcludeIfLessThanIntegerRule extends AbstractExcludeIntegerRule {
public ExcludeIfLessThanIntegerRule(int threshold, ExclusionRule<Integer> or) {
super(threshold, or);
}
public ExcludeIfLessThanIntegerRule(int threshold) {
this(threshold, null);
}
#Override
protected boolean doComparison(int x, int threshold) {
return x < threshold;
}
}
public class ExcludeIfEqualIntegerRule extends AbstractExcludeIntegerRule {
public ExcludeIfEqualIntegerRule(int threshold, ExclusionRule<Integer> or) {
super(threshold, or);
}
public ExcludeIfEqualIntegerRule(int threshold) {
this(threshold, null);
}
#Override
protected boolean doComparison(int x, int threshold) {
return x == threshold;
}
}
Along with another class which defines the set of rules to evaluate any candidate permutation:
public class ExclusionEvaluator<T, U, V> {
private final ExclusionRule<T> tRule;
private final ExclusionRule<U> uRule;
private final ExclusionRule<V> vRule;
public ExclusionEvaluator(ExclusionRule<T> tRule, ExclusionRule<U> uRule, ExclusionRule<V> vRule) {
this.tRule = tRule;
this.uRule = uRule;
this.vRule = vRule;
}
public boolean isExcluded(T t, U u, V v) {
return tRule.isExcluded(t) && uRule.isExcluded(u) && vRule.isExcluded(v);
}
}
With the three lists as separate objects, this can be encapsulated within another class which provides the getCombinations() method:
public class PermutationProvider<T, U, V> {
private final List<T> tItems;
private final List<U> uItems;
private final List<V> vItems;
private final List<ExclusionEvaluator<T, U, V>> exclusionEvaluators;
public PermutationProvider(List<T> tItems, List<U> uItems, List<V> vItems, List<ExclusionEvaluator<T, U, V>> exclusionEvaluators) {
this.tItems = tItems;
this.uItems = uItems;
this.vItems = vItems;
this.exclusionEvaluators = exclusionEvaluators;
}
public List<Permutation<T, U, V>> getCombinations() {
List<Permutation<T, U, V>> combinations = new ArrayList<>();
for (T tElement : tItems) {
for (U uElement : uItems) {
for (V vElement : vItems) {
Permutation<T, U, V> p = new Permutation<>(tElement, uElement, vElement);
if (isExcluded(tElement, uElement, vElement)) {
System.out.println(p + " IS EXCLUDED");
} else {
combinations.add(p);
}
}
}
}
return combinations;
}
private boolean isExcluded(T tElement, U uElement, V vElement) {
for (ExclusionEvaluator<T, U, V> exclusionEvaluator : exclusionEvaluators) {
if (exclusionEvaluator.isExcluded(tElement, uElement, vElement)) {
return true;
}
}
return false;
}
}
and result class to hold a permutation:
public class Permutation<T, U, V> {
private final T t;
private final U u;
private final V v;
public Permutation(T t, U u, V v) {
this.t = t;
this.u = u;
this.v = v;
}
public String toString() {
return t.toString() + " " + u.toString() + " " + v.toString();
}
}
along with a driver class to build the lists, exclusion rules, and get the accepted permutations:
public class PermuteWithExclusionsApp {
public static void main(String[] args) {
new PermuteWithExclusionsApp().permute();
}
private void permute() {
List<String> p1 = Arrays.asList("Lion", "Chair", "Snow");
List<Integer> p2 = new ArrayList<>();
for (int i = 0; i <= 28; i++) {
p2.add(i);
}
List<Integer> p3 = new ArrayList<>();
for (int i = 34; i <= 39; i++) {
p3.add(i);
}
// read from a file or some other source
List<String> compoundExclusionRules = Arrays.asList("P1 = Lion && P2 > 23 && P3 <= 35", "P1 = Lion && P2 < 13 && P3 >= 37", "P1 = Chair && P2 < 7 && P3 = 34");
ExclusionRuleFactory<String> stringRuleFactory = new StringExclusionRuleFactory();
ExclusionRuleFactory<Integer> integerRuleFactory = new IntegerExclusionRuleFactory();
ExclusionEvaluatorFactory<String, Integer, Integer> evaluatorFactory = new ExclusionEvaluatorFactory<>(stringRuleFactory, integerRuleFactory, integerRuleFactory);
List<ExclusionEvaluator<String, Integer, Integer>> evaluators = new ArrayList<>();
for (String compoundExclusionRule : compoundExclusionRules) {
evaluators.add(evaluatorFactory.create(compoundExclusionRule));
}
// List<ExclusionEvaluator<String, Integer, Integer>> evaluators = new ArrayList<>();
// evaluators.add(getExclusionRul1());
// evaluators.add(getExclusionRul2());
// evaluators.add(getExclusionRul3());
PermutationProvider<String, Integer, Integer> provider = new PermutationProvider<>(p1, p2, p3, evaluators);
List<Permutation<String, Integer, Integer>> permuations = provider.getCombinations();
for (Permutation<String, Integer, Integer> p : permuations) {
System.out.println(p);
}
}
// private ExclusionEvaluator<String, Integer, Integer> getExclusionRul3() {
// ExclusionRule<String> p1Rule = new ExcludeIfEqualStringRule("Chair");
// ExclusionRule<Integer> p2Rule = new ExcludeIfLessThanIntegerRule(7);
// ExclusionRule<Integer> p3Rule = new ExcludeIfEqualIntegerRule(34);
// return new ExclusionEvaluator<String, Integer, Integer>(p1Rule, p2Rule, p3Rule);
// }
//
// private ExclusionEvaluator<String, Integer, Integer> getExclusionRul2() {
// ExclusionRule<String> p1Rule = new ExcludeIfEqualStringRule("Lion");
// ExclusionRule<Integer> p2Rule = new ExcludeIfLessThanIntegerRule(13);
// ExclusionRule<Integer> p3Rule = new ExcludeIfGreaterThanIntegerRule(37, new ExcludeIfEqualIntegerRule(37));
// return new ExclusionEvaluator<String, Integer, Integer>(p1Rule, p2Rule, p3Rule);
// }
//
// private ExclusionEvaluator<String, Integer, Integer> getExclusionRul1() {
// ExclusionRule<String> p1Rule = new ExcludeIfEqualStringRule("Lion");
// ExclusionRule<Integer> p2Rule = new ExcludeIfGreaterThanIntegerRule(23);
// ExclusionRule<Integer> p3Rule = new ExcludeIfLessThanIntegerRule(35, new ExcludeIfEqualIntegerRule(35));
// return new ExclusionEvaluator<String, Integer, Integer>(p1Rule, p2Rule, p3Rule);
// }
Here's an example of a factory to parse the exclusion rules if for example the rules were defined as text.
public interface ExclusionRuleFactory<Z> {
public ExclusionRule<Z> create(String operator, String operand);
}
public class StringExclusionRuleFactory implements ExclusionRuleFactory<String> {
#Override
public ExclusionRule<String> create(String operator, String operand) {
return new ExcludeIfEqualStringRule(operand);
}
}
public class IntegerExclusionRuleFactory implements ExclusionRuleFactory<Integer> {
#Override
public ExclusionRule<Integer> create(String operator, String operand) {
int threshold = Integer.parseInt(operand);
switch (operator) {
case "=":
return new ExcludeIfEqualIntegerRule(threshold);
case ">":
return new ExcludeIfGreaterThanIntegerRule(threshold);
case "<":
return new ExcludeIfLessThanIntegerRule(threshold);
case ">=":
return new ExcludeIfGreaterThanIntegerRule(threshold, new ExcludeIfEqualIntegerRule(threshold));
case "<=":
return new ExcludeIfLessThanIntegerRule(threshold, new ExcludeIfEqualIntegerRule(threshold));
}
throw new IllegalArgumentException("Unsupported operator " + operator);
}
}
public class ExclusionEvaluatorFactory<T, U, V> {
private final ExclusionRuleFactory<T> p1RuleFactory;
private final ExclusionRuleFactory<U> p2RuleFactory;
private final ExclusionRuleFactory<V> p3RuleFactory;
public ExclusionEvaluatorFactory(ExclusionRuleFactory<T> p1RuleFactory, ExclusionRuleFactory<U> p2RuleFactory, ExclusionRuleFactory<V> p3RuleFactory) {
this.p1RuleFactory = p1RuleFactory;
this.p2RuleFactory = p2RuleFactory;
this.p3RuleFactory = p3RuleFactory;
}
public ExclusionEvaluator<T, U, V> create(String compoundExclusionRule) {
ExclusionRule<T> p1Rule = null;
ExclusionRule<U> p2Rule = null;
ExclusionRule<V> p3Rule = null;
String[] exclusionSubRules = compoundExclusionRule.split("&&");
for (int sr = 0; sr < exclusionSubRules.length; sr++) {
String[] ruleParts = exclusionSubRules[sr].trim().split(" ");
String whichRule = ruleParts[0].trim();
String operator = ruleParts[1].trim();
String operand = ruleParts[2].trim();
switch (whichRule) {
case "P1":
p1Rule = p1RuleFactory.create(operator, operand);
break;
case "P2":
p2Rule = p2RuleFactory.create(operator, operand);
break;
case "P3":
p3Rule = p3RuleFactory.create(operator, operand);
break;
}
}
return new ExclusionEvaluator<T, U, V>(p1Rule, p2Rule, p3Rule);
}
}

You could pass a Predicate function to your getCombinations method to filter or accept certain combinations:
/** Returns true if this combination is accepted, false if it should be filtered. */
public static boolean myFilter(Object[] combo) {
// Object arrays and casting is gross
if (combo.length != 3) {
return false;
}
try {
String p1 = (String) combo[0];
// Why are they strings?
Integer p2 = Integer.valueOf((String) combo[1]);
Integer p3 = Integer.valueOf((String) combo[2]);
return !("Lion".equals(p1) && (13 > p2) && (35 >= p3))
&& !("Lion".equals(p1) && (13 > p2) && (37 <= p3))
&& !("Chair".equals(p1) && (7 > p2) && (34 == p3));
} catch (Exception e) {
// invalid combination, filter it
return false;
}
}
#SuppressWarnings("unchecked")
public static <T> List<List<T>> getCombinations(
Collection<? extends Iterable<T>> valueSetCollection,
Predicate<Object[]> filter) {
Iterable<T>[] valueSets = new Iterable[valueSetCollection.size()];
Iterator<T>[] valueIters = new Iterator[valueSetCollection.size()];
T[] values = (T[]) new Object[valueSetCollection.size()];
int i = 0;
for (Iterable<T> valueSet : valueSetCollection) {
valueSets[i] = valueSet; // Copy to array for fast index lookup
valueIters[i] = valueSet.iterator();
values[i] = valueIters[i].next(); // Fail if a wordSet is empty
i++;
}
List<List<T>> combinations = new ArrayList<>();
NEXT_COMBO: for (;;) {
T[] v = values.clone();
if (filter.test(v)) {
combinations.add(Arrays.asList(v));
} else {
System.out.println("rejected " + Arrays.asList(v));
}
for (i = values.length - 1; i >= 0; i--) {
if (valueIters[i].hasNext()) {
values[i] = valueIters[i].next();
continue NEXT_COMBO;
}
valueIters[i] = valueSets[i].iterator();
values[i] = valueIters[i].next();
}
return combinations;
}
}
public static void main(String[] args){
// ...
getCombinations(rows, MyClass::myFilter).forEach(System.out::println);
System.out.println("##############################");
// accept all
getCombinations(rows, o -> true).forEach(System.out::println);
}
Also, rather than passing lists of lists it may be better to make a data container class like this:
public class MyCombination {
private final String s;
private final int i1;
private final int i2;
public MyCombination(String s, int i1, int i2){
this.s = s;
this.i1 = i1;
this.i2 = i2;
}
}

Related

Modifying Dijkstra to save paths with equal values - StackOverflow error

I'm trying to modify Dijkstra's algorithm to show all the paths that have the minimum value. So I decided to use a list of previous vertices. And I added an if clause that checks if the path is igual to the previous with minimum value I add the previous vertex as the parent of the current one.
The problem is that I am getting a StackOverflow error and I don't know what is causing it.
This is my code:
The purpose of the code below is to calculate Dijkstra for all of the vertices in the graph, calculate the number of times a vertex appears in the minimum paths found and display in decrescent order the all of them.
public class Dijkstra {
public static final Map<String, Integer> ordem = new HashMap<>();
public static void main(String[] args) throws FileNotFoundException, IOException {
List<Graph.Edge> edges = new ArrayList<>();
try {
FileReader arq = new FileReader("input.txt");
BufferedReader fw = new BufferedReader(arq);
String linha = "";
while (fw.ready()) {
linha = fw.readLine();
if (!linha.equals("0,0,0")) {
String parts[] = linha.split(",");
ordem.put(parts[0], 0);
ordem.put(parts[1], 0);
Graph.Edge a = new Graph.Edge(parts[0], parts[1], 100 - Integer.parseInt(parts[2]));
edges.add(a);
} else {
break;
}
}
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Graph g = new Graph(edges);
for (int i = 0; i < 5; i++) {
g.dijkstra(String.valueOf(i));
g.printAllPaths();
}
Object[] a = ordem.entrySet().toArray();
Arrays.sort(a, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Map.Entry<String, Integer>) o2).getValue()
.compareTo(((Map.Entry<String, Integer>) o1).getValue());
}
});
for (Object e : a) {
System.out.print(((Map.Entry<String, Integer>) e).getKey() + " ");
}
System.out.println("\n");
}
}
class Graph {
private final Map<String, Vertex> graph;
public static class Edge {
public final String v1, v2;
public final int dist;
public Edge(String v1, String v2, int dist) {
this.v1 = v1;
this.v2 = v2;
this.dist = dist;
}
}
public static class Vertex implements Comparable<Vertex> {
public final String name;
public int dist = Integer.MAX_VALUE;
public List<Vertex> previous = new ArrayList<>();
public final Map<Vertex, Integer> neighbours = new HashMap<>();
public Vertex(String name) {
this.name = name;
}
private void printPath() {
if (this == this.previous) {
} else if (this.previous == null) {
} else {
//This is where I am getting the error
for (int i = 0; i<this.previous.size(); i++){
this.previous.get(i).printPath();
Dijkstra.ordem.replace(this.name, Dijkstra.ordem.get(this.name) + 1);
}
}
}
public int compareTo(Vertex other) {
if (dist == other.dist) {
return name.compareTo(other.name);
}
return Integer.compare(dist, other.dist);
}
#Override
public String toString() {
return "(" + name + ", " + dist + ")";
}
}
public Graph(List<Graph.Edge> edges) {
graph = new HashMap<>(edges.size());
for (Edge e : edges) {
if (!graph.containsKey(e.v1)) {
graph.put(e.v1, new Vertex(e.v1));
}
if (!graph.containsKey(e.v2)) {
graph.put(e.v2, new Vertex(e.v2));
}
}
for (Edge e : edges) {
graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist);
}
}
public void dijkstra(String startName) {
if (!graph.containsKey(startName)) {
System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
return;
}
final Vertex source = graph.get(startName);
NavigableSet<Vertex> q = new TreeSet<>();
// Inicializacao dos vertices
for (Vertex v : graph.values()) {
//v.previous = v == source ? list : null;
if (v == source) {
v.previous.add(source);
} else {
v.previous = new ArrayList<>();
}
v.dist = v == source ? 0 : Integer.MAX_VALUE;
q.add(v);
}
dijkstra(q);
}
private void dijkstra(final NavigableSet<Vertex> q) {
Vertex u, v;
while (!q.isEmpty()) {
u = q.pollFirst();
if (u.dist == Integer.MAX_VALUE) {
}
for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
v = a.getKey();
final int alternateDist = u.dist + a.getValue();
if (alternateDist < v.dist) {
q.remove(v);
v.dist = alternateDist;
v.previous.add(u);
q.add(v);
} else if(alternateDist == v.dist) {
v.previous.add(u);
}
}
}
}
public void printPath(String endName) {
if (!graph.containsKey(endName)) {
System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName);
return;
}
graph.get(endName).printPath();
//System.out.println();
}
public void printAllPaths() {
for (Vertex v : graph.values()) {
v.printPath();
}
}
}
This is the error:
Exception in thread "main" java.lang.StackOverflowError
at Graph$Vertex.printPath(Dijkstra.java:117)
at Graph$Vertex.printPath(Dijkstra.java:118)
As the Error message already suggests: Your dijkstra isn't the problem.
The problem is printPath() calling itself.
Likely the culprit is
if (this == this.previous) {} ...
You compare Vertex this to List<Vertex> previous. Maybe you want to check
if (this == this.previous.get(this.previous.size() - 1)) {} ...
instead.
I didn't test this, because your code is (1.) too long and (2.) not self-contained (missing at least "input.txt").

Is it possible to box user-defined Datatypes in more than 1 Level

I am working on a project with a datatructure that is deeper than 1 level and I try to box different user-defined datatypes to hold information of all levels.
First Level: A class that has only an ArrayList of level 2 datatype and some functions to access the data of all Levels.
Second Level: A class that has some standard variables and an ArrayList of Level 3 datatype and also some functions .
Third Level: A class / record of some standard variables
The instance of the first-level datatype should be used by more than one activity of my application (almost all).
Here is my problem: I can initialize the first-level and add some objects to the list, but I could not do so with the second level. If I add some object in the list of the second level all seems to be good, but when I access this data a NullPointerException occurs.
Here is my question: Is it possible to box datatypes in more than 1 level? And where I should search for the error.
For better understanding the sources:
modulData.java (First Level)
package de.myApp.dataholder;
import java.util.ArrayList;
import android.util.Log;
public class modulData {
// Constructor
private static final modulData mainModulDataHolder = new modulData();
public static modulData getInstance() {return mainModulDataHolder;}
// Data
private ArrayList<isModulData> mIsModulData;
// Functions
public void init() {
this.mIsModulData = new ArrayList<isModulData>();
}
public isModulData getModul(int mID) {
isModulData result = null;
int i;
for (i = 0; i < mIsModulData.size(); i++) {
if (mIsModulData.get(i).ID() == mID) {
result = mIsModulData.get(i);
break;
}
}
return result;
}
public isCatData getCategory(int mID, int cID) {
isCatData result = new isCatData();
isCatData buffer = new isCatData();
int i,j ;
for (i = 0; i < mIsModulData.size(); i++) {
if (mIsModulData.get(i).ID() == mID) {
for(j = 0; j < mIsModulData.get(i).CountCategorys(); j++) {
buffer = mIsModulData.get(i).getCategory(j);
if (buffer.ID() == cID) {
result = buffer;
break;
}
}
}
}
return result;
}
public int ModulCount() {
int result = 0;
if (mIsModulData != null) { result = mIsModulData.size(); }
return result;
}
public int CategoryCount(int mID) {
int result = -1;
int i;
for (i = 0; i < mIsModulData.size(); i++) {
if (mIsModulData.get(i).ID() == mID) {
result = mIsModulData.get(i).CountCategorys();
break;
}
}
return result;
}
public int addModulWithJSON(String jsonString) {
int result = -1;
isModulData newModul = new isModulData();
newModul.initCategorys();
if (mIsModulData == null) { this.init(); }
result = newModul.decodeFromJSON(jsonString);
mIsModulData.add(newModul);
return result;
}
public int addCategoryWithJSON(int mID, String jsonString) {
int result = -1;
int i;
for (i = 0; i < mIsModulData.size(); i++){
if (mIsModulData.get(i).ID() == mID) {
result = mIsModulData.get(i).addCategoryWithJSON(jsonString);
Log.i("AddCategoryWithJSON","Add Category "+ String.valueOf(result));
// Here is the Problem: Added but no access to Data. Null Object reference !!!!
Log.i("AddCategoryWithJSON", mIsModulData.get(mID).getCategory(result).Title());
break;
}
}
return result;
}
}
isModulData.java (second level)
package de.myApp.dataholder;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class isModulData {
// Constructor
private static final isModulData modulDataHolder = new isModulData();
public static isModulData getInstance() {return modulDataHolder;}
// Data
private ArrayList<isCatData> mIsCatData;
private int mID;
private String mTitle;
private boolean misActive;
private int mBackgroundID;
// Functions
public int ID() { return this.mID; }
public String Title() { return this.mTitle; }
public boolean ActiveState() { return this.misActive; }
public int BackgroundID() { return this.mBackgroundID; }
public int decodeFromJSON(String jsonString) {
int result;
try {
JSONArray jArray = new JSONArray(jsonString);
this.mID = Integer.parseInt(jArray.getString(0).toString());
this.mTitle = jArray.getString(1).toString();
this.misActive = Boolean.parseBoolean(jArray.getString(2).toString());
this.mBackgroundID = Integer.parseInt(jArray.getString(3).toString());
result = this.mID;
} catch (JSONException e) {
e.printStackTrace();
Log.w("JSONDecode CMD801","Decode raised in JSONException ["+e.toString()+"]");
result = -1;
}
return result;
}
public int addCategoryWithJSON(String jsonString) {
int result = -1;
if (this.mIsCatData == null) { this.initCategorys(); }
isCatData cNewCategory = new isCatData();
result = cNewCategory.decodeFromJSON(jsonString);
this.mIsCatData.add(cNewCategory);
return result;
}
public isCatData getCategory(int cID) {
isCatData result = new isCatData();
if (cID < mIsCatData.size() -1) {
result = mIsCatData.get(cID);
} else {
result = null;
}
return result;
}
public void initCategorys() {
this.mIsCatData = new ArrayList<isCatData>();
}
public int CountCategorys() {
return this.mIsCatData.size();
}
}
isCatData.java (third level)
package de.myApp.dataholder;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class isCatData {
// Constructor
private static final isCatData catDataHolder = new isCatData();
public static isCatData getInstance() {return catDataHolder;}
// Data
private int cID;
private String cTitle;
private boolean cisActive;
private boolean cisWriteEnable;
private int cBackgroundID;
// Functions
public int ID() { return this.cID; }
public String Title() { return this.cTitle; }
public boolean ActiveState() { return this.cisActive; }
public boolean WriteEnableState() { return this.cisWriteEnable; }
public int BackgroundID() { return this.cBackgroundID; }
public int decodeFromJSON(String jsonString) {
int result;
try {
JSONArray jArray = new JSONArray(jsonString);
this.cID = Integer.parseInt(jArray.getString(0).toString());
this.cTitle = jArray.getString(1).toString();
this.cisActive = Boolean.parseBoolean(jArray.getString(2).toString());
this.cisWriteEnable = Boolean.parseBoolean(jArray.getString(3).toString());
this.cBackgroundID = Integer.parseInt(jArray.getString(4).toString());
result = this.cID;
} catch (JSONException e) {
e.printStackTrace();
Log.w("JSONDecode CMD801","Decode raised in JSONException ["+e.toString()+"]");
result = -1;
}
return result;
}
}

Performing multiple computations with Hadoop Map Reduce

I have a map reduce program for finding the min/max for 2 separate properties for each year. This works, for the most part, using a single node cluster in hadoop. Here is my currently setup:
public class MaxTemperatureReducer extends
Reducer<Text, Stats, Text, Stats> {
private Stats result = new Stats();
#Override
public void reduce(Text key, Iterable<Stats> values, Context context)
throws IOException, InterruptedException {
int maxValue = Integer.MIN_VALUE;
int minValue = Integer.MAX_VALUE;
int sum = 0;
for (Stats value : values) {
result.setMaxTemp(Math.max(maxValue, value.getMaxTemp()));
result.setMinTemp(Math.min(minValue, value.getMinTemp()));
result.setMaxWind(Math.max(maxValue, value.getMaxWind()));
result.setMinWind(Math.min(minValue, value.getMinWind()));
sum += value.getCount();
}
result.setCount(sum);
context.write(key, result);
}
}
public class MaxTemperatureMapper extends
Mapper<Object, Text, Text, Stats> {
private static final int MISSING = 9999;
private Stats outStat = new Stats();
#Override
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String[] split = value.toString().split("\\s+");
String year = split[2].substring(0, 4);
int airTemperature;
airTemperature = (int) Float.parseFloat(split[3]);
outStat.setMinTemp((float)airTemperature);
outStat.setMaxTemp((float)airTemperature);
outStat.setMinWind(Float.parseFloat(split[12]));
outStat.setMaxWind(Float.parseFloat(split[14]));
outStat.setCount(1);
context.write(new Text(year), outStat);
}
}
public class MaxTemperatureDriver extends Configured implements Tool {
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.err
.println("Usage: MaxTemperatureDriver <input path> <outputpath>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(MaxTemperatureDriver.class);
job.setJobName("Max Temperature");
job.setMapperClass(MaxTemperatureMapper.class);
job.setCombinerClass(MaxTemperatureReducer.class);
job.setReducerClass(MaxTemperatureReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Stats.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
boolean success = job.waitForCompletion(true);
return success ? 0 : 1;
}
public static void main(String[] args) throws Exception {
MaxTemperatureDriver driver = new MaxTemperatureDriver();
int exitCode = ToolRunner.run(driver, args);
System.exit(exitCode);
}
}
Currently it only prints the Min/Max for the temp and windspeed for each year. I am sure it is a simple implementation but cannot find a answer anywhere. I want to try and find the top 5 min/max for each year. Any suggestions?
Let me assume the following signature for your Stats class.
/* the stats class need to be a writable, the below is just a demo*/
public class Stats {
public float getTemp() {
return temp;
}
public void setTemp(float temp) {
this.temp = temp;
}
public float getWind() {
return wind;
}
public void setWind(float wind) {
this.wind = wind;
}
private float temp;
private float wind;
}
With this, let us change the reducer as below.
SortedSet<Float> tempSetMax = new TreeSet<Float>();
SortedSet<Float> tempSetMin = new TreeSet<Float>();
SortedSet<Float> windSetMin = new TreeSet<Float>();
SortedSet<Float> windSetMax = new TreeSet<Float>();
List<Stats> values = new ArrayList<Float>();
for (Stats value : values) {
float temp = value.getTemp();
float wind = value.getWind();
if (tempSetMax.size() < 5) {
tempSetMax.add(temp);
} else {
float currentMinValue = tempSetMax.first();
if (temp > currentMinValue) {
tempSetMax.remove(currentMinValue);
tempSetMax.add(temp);
}
}
if (tempSetMin.size() < 5) {
tempSetMin.add(temp);
} else {
float currentMaxValue = tempSetMin.last();
if (temp < currentMaxValue) {
tempSetMax.remove(currentMaxValue);
tempSetMax.add(temp);
}
}
if (windSetMin.size() < 5) {
windSetMin.add(wind);
} else {
float currentMaxValue = windSetMin.last();
if (wind < currentMaxValue) {
windSetMin.remove(currentMaxValue);
windSetMin.add(temp);
}
}
if (windSetMax.size() < 5) {
windSetMax.add(wind);
} else {
float currentMinValue = windSetMax.first();
if (wind > currentMinValue) {
windSetMax.remove(currentMinValue);
windSetMax.add(temp);
}
}
}
Now you can write to context the toString() of each list, or you can create a custom writable. In my code, please change the Stats according to your requirement. It needs to be a Writable. The above is just for demonstrating the example flow.
Here is the code from the MR Design Patterns Book to get the top 10. There is also code for other MR design patterns in the same GitHub location.

3 dimensional ConcurrentSkipListMap map

I have written 3 dimensional ConcurrentSkipListMap, but not able to figure out a way to iterate over it. How do i define a iterator for the same.
import java.util.concurrent.ConcurrentSkipListMap;
/**
* Helper implementation to handle 3 dimensional sorted maps
*/
public class MyCustomIndex {
private ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], byte[]>>> table;
public MyCustomIndex() {
this.table = new ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], byte[]>>>(new CustomComparator);
}
/**
*
* #param K
* #param F
* #param Q
*/
public void put(byte[] K, byte[] F, byte[] Q) {
ConcurrentSkipListMap<byte[], byte[]> QToDummyValueMap;
ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], byte[]>> FToQMap;
if( table.containsK(K)) {
FToQMap = table.get(K);
if ( FToQMap.containsK(F)) {
QToDummyValueMap = FToQMap.get(F);
} else {
QToDummyValueMap = new ConcurrentSkipListMap<byte[], byte[]>(new CustomComparator);
}
} else {
QToDummyValueMap = new ConcurrentSkipListMap<byte[], byte[]>(new CustomComparator);
FToQMap = new ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], byte[]>>(new CustomComparator);
}
QToDummyValueMap.put(Q, new byte[0]);
FToQMap.put(F, QToDummyValueMap);
table.put(K, FToQMap);
}
public ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], byte[]>>> gettable() {
return table;
}
public void settable(
ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], ConcurrentSkipListMap<byte[], byte[]>>> table) {
this.table = table;
}
}
Here's a nested iterator that will take an Iterator<Iterator<T>> and turn it into an Iterator<T>. Also there are static methods that allow you to grow Iterator<V> objects out of Map<K,V>s along with some higher-order stuff (Iterator<Iterator<V>> and Map<K,Map<K,V> etc.).
The idea for iterating over higher levels of iterator such as Iterator<Iterator<Iterator<T>>> would be to wrap one of these inside the other as you can see in the three-way test and the MapMapMap test.
public class NestedIterator<T> implements Iterator<T> {
// Outer iterator. Goes null when exhausted.
Iterator<Iterator<T>> i2 = null;
// Inner iterator. Goes null when exhausted.
Iterator<T> i1 = null;
// Next value.
T next = null;
// Takes a depth-2 iterator.
public NestedIterator(Iterator<Iterator<T>> i2) {
this.i2 = i2;
// Prime the pump.
if (i2 != null && i2.hasNext()) {
i1 = i2.next();
}
}
#Override
public boolean hasNext() {
// Is there one waiting?
if (next == null) {
// No!
// i1 will go null if it is exhausted.
if (i1 == null) {
// i1 is exhausted! Get a new one from i2.
if (i2 != null && i2.hasNext()) {
/// Get next.
i1 = i2.next();
// Set i2 null if exhausted.
if (!i2.hasNext()) {
// Exhausted.
i2 = null;
}
} else {
// Exhausted.
i2 = null;
}
}
// A null i1 now will mean all is over!
if (i1 != null) {
if (i1.hasNext()) {
// get next.
next = i1.next();
// Set i1 null if exhausted.
if (!i1.hasNext()) {
// Exhausted.
i1 = null;
}
} else {
// Exhausted.
i1 = null;
}
}
}
return next != null;
}
#Override
public T next() {
T n = next;
next = null;
return n;
}
#Override
public void remove() {
throw new UnsupportedOperationException("Not supported.");
}
// Iterating across Maps of Maps of Maps.
static <K1, K2, K3, V> Iterator<Iterator<Iterator<V>>> iiiV(Map<K1, Map<K2, Map<K3, V>>> i) {
final Iterator<Map<K2, Map<K3, V>>> iV = iV(i);
return new Iterator<Iterator<Iterator<V>>>() {
#Override
public boolean hasNext() {
return iV.hasNext();
}
#Override
public Iterator<Iterator<V>> next() {
return iiV(iV.next());
}
#Override
public void remove() {
iV.remove();
}
};
}
// Iterating across Maps of Maps.
static <K1, K2, V> Iterator<Iterator<V>> iiV(Map<K1, Map<K2, V>> i) {
final Iterator<Map<K2, V>> iV = iV(i);
return new Iterator<Iterator<V>>() {
#Override
public boolean hasNext() {
return iV.hasNext();
}
#Override
public Iterator<V> next() {
return iV(iV.next());
}
#Override
public void remove() {
iV.remove();
}
};
}
// Iterating across Map values.
static <K, V> Iterator<V> iV(final Map<K, V> map) {
return iV(map.entrySet().iterator());
}
// Iterating across Map.Entry Iterators.
static <K, V> Iterator<V> iV(final Iterator<Map.Entry<K, V>> i) {
return new Iterator<V>() {
#Override
public boolean hasNext() {
return i.hasNext();
}
#Override
public V next() {
return i.next().getValue();
}
#Override
public void remove() {
i.remove();
}
};
}
// **** TESTING ****
enum I {
I1, I2, I3;
};
public static void main(String[] args) {
// Two way test.
testTwoWay();
System.out.flush();
System.err.flush();
// Three way test.
testThreeWay();
System.out.flush();
System.err.flush();
// MapMap test
testMapMap();
System.out.flush();
System.err.flush();
// MapMapMap test
testMapMapMap();
System.out.flush();
System.err.flush();
}
private static void testMapMap() {
Map<String,String> m = new TreeMap<> ();
m.put("M-1", "V-1");
m.put("M-2", "V-2");
Map<String,Map<String,String>> mm = new TreeMap<> ();
mm.put("MM-1", m);
mm.put("MM-2", m);
System.out.println("MapMap");
Iterator<Iterator<String>> iiV = iiV(mm);
for (Iterator<String> i = new NestedIterator<>(iiV); i.hasNext();) {
System.out.print(i.next() + ",");
}
System.out.println();
}
private static void testMapMapMap() {
Map<String,String> m = new TreeMap<> ();
m.put("M-1", "V-1");
m.put("M-2", "V-2");
m.put("M-3", "V-3");
Map<String,Map<String,String>> mm = new TreeMap<> ();
mm.put("MM-1", m);
mm.put("MM-2", m);
Map<String,Map<String,Map<String,String>>> mmm = new TreeMap<> ();
mmm.put("MMM-1", mm);
mmm.put("MMM-2", mm);
System.out.println("MapMapMap");
Iterator<Iterator<Iterator<String>>> iiiV = iiiV(mmm);
for (Iterator<String> i = new NestedIterator<>(new NestedIterator<>(iiiV)); i.hasNext();) {
System.out.print(i.next() + ",");
}
System.out.println();
}
private static void testThreeWay() {
// Three way test.
System.out.println("Three way");
List<Iterator<I>> lii1 = Arrays.asList(
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator());
List<Iterator<I>> lii2 = Arrays.asList(
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator());
List<Iterator<I>> lii3 = Arrays.asList(
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator());
Iterator<Iterator<Iterator<I>>> liii = Arrays.asList(
lii1.iterator(),
lii2.iterator(),
lii3.iterator()).iterator();
// Grow a 3-nest.
// Unroll it.
for (Iterator<I> ii = new NestedIterator<>(new NestedIterator<>(liii)); ii.hasNext();) {
I it = ii.next();
System.out.print(it + ",");
}
System.out.println();
}
private static void testTwoWay() {
System.out.println("Two way");
List<Iterator<I>> lii = Arrays.asList(
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator(),
EnumSet.allOf(I.class).iterator());
for (Iterator<I> ii = new NestedIterator<>(lii.iterator()); ii.hasNext();) {
I it = ii.next();
System.out.print(it + ",");
}
System.out.println();
}
}
Your code can now look something like this. Note that I have not tested this at all and I have made use of Map instead of ConcurrentSkipListMap whenever possible and I am using the <> stuff from Java 7 to help out a LOT.
public class MyCustomIndex implements Iterable<byte[]> {
private Map<byte[], Map<byte[], Map<byte[], byte[]>>> table;
public MyCustomIndex() {
this.table = new ConcurrentSkipListMap<>();
}
/**
* #param K
* #param F
* #param Q
*/
public void put(byte[] K, byte[] F, byte[] Q) {
Map<byte[], byte[]> QToDummyValueMap;
Map<byte[], Map<byte[], byte[]>> FToQMap;
if (table.containsKey(K)) {
FToQMap = table.get(K);
if (FToQMap.containsKey(F)) {
QToDummyValueMap = FToQMap.get(F);
} else {
QToDummyValueMap = new ConcurrentSkipListMap<>();
}
} else {
QToDummyValueMap = new ConcurrentSkipListMap<>();
FToQMap = new ConcurrentSkipListMap<>();
}
QToDummyValueMap.put(Q, new byte[0]);
FToQMap.put(F, QToDummyValueMap);
table.put(K, FToQMap);
}
public Map<byte[], Map<byte[], Map<byte[], byte[]>>> gettable() {
return table;
}
public Iterator<byte[]> iterator () {
// **** This is what I have been aiming at all along ****
return new NestedIterator(new NestedIterator<>(NestedIterator.iiiV(table)));
}
}

Java obj cloning problem

I would need some help with the following code if you are kind.
Basically i have a tree node that remembers it's parent node, depth-level and his current state(a 2D array).
Most variable names are written in my native language, i hope this is not a problem :
public class Nod implements Cloneable {
private Nod parinte;//parent node
private int[][] stare;//state
private int cost;//depth-level
private String actiune;//the action used to obtain this node
private volatile int hashCode = 0;
public boolean equals(Object obj)
{
if(this == obj)
{
return true;
}
if (!(obj instanceof Nod))
{
return false;
}
Nod nod = (Nod)obj;
return cost == nod.getCost() && actiune.equals(nod.getActiune())
&& stare.equals(nod.getStareNod());
}
public int hashCode()
{
StringBuffer strBuff = new StringBuffer();
try
{
int n = Problema.Dimensiune();//returns the dimension of state matrix
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
strBuff.append(stare[i][j]);
strBuff.append(cost);
strBuff.append(actiune);
String str = strBuff.toString();
hashCode = str.hashCode();
}
catch (IOException e) {
e.printStackTrace();
}
return hashCode;
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
public static boolean goUp(int[][] st) throws IOException
{
int n = Problema.Dimensiune();
boolean ok = false;
int[][] a = st;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{
if (a[i][j] == 0)
if (i != 0)
ok = true;
}
return ok;
}
public static boolean goDown(int[][] st) throws IOException
{
int n = Problema.Dimensiune();
boolean ok = false;
int[][] a = st;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{
if (a[i][j] == 0)
if (i != (n-1))
ok = true;
}
return ok;
}
public static boolean goLeft(int[][] st) throws IOException
{
int n = Problema.Dimensiune();
boolean ok = false;
int[][] a = st;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{
if (a[i][j] == 0)
if (j != 0)
ok = true;
}
return ok;
}
public static boolean goRight(int[][] st) throws IOException
{
int n = Problema.Dimensiune();
boolean ok = false;
int[][] a = st;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
{
if (a[i][j] == 0)
if (j != (n-1))
ok = true;
}
return ok;
}
public static int[] Zero(int[][] st) throws IOException
{
int[][] a = st;
int n = Problema.Dimensiune();
int[] b = new int[2];
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (a[i][j] == 0)
{
b[0] = i;
b[1] = j;
}
return b;
}
public static int[][] Actiune(int[][] st, String s) throws IOException
{
int[][] a = st;
int[] b = Zero(st);
if ((goRight(st) == true) && (s == "right"))
{
a[b[0]][b[1]] = a[b[0]][b[1]+1];
a[b[0]][b[1]+1] = 0;
}
if ((goLeft(st) == true) && (s == "left"))
{
a[b[0]][b[1]] = a[b[0]][b[1]-1];
a[b[0]][b[1]-1] = 0;
}
if ((goUp(st) == true) && (s == "up"))
{
a[b[0]][b[1]] = a[b[0]-1][b[1]];
a[b[0]-1][b[1]] = 0;
}
if ((goDown(st) == true) && (s == "down"))
{
a[b[0]][b[1]] = a[b[0]+1][b[1]];
a[b[0]+1][b[1]] = 0;
}
return a;
}
public Nod(){}
public Nod (int[][] st)
{
parinte = null;
stare = st;
cost = 0;
actiune = null;
}
public Nod (Nod nod)
{
parinte = nod.parinte;
stare = nod.stare;
cost = nod.cost;
actiune = nod.actiune;
}
public Nod(Nod nodp, String ac) throws IOException
{
this.parinte = nodp;
this.cost = parinte.getCost()+1;
this.actiune = ac;
this.stare = Actiune(parinte.getStareNod(),actiune);
}
public void setCost(int cost)
{
this.cost = cost;
}
public int getCost(){
return this.cost;
}
public void setStareNod(int[][] stare)
{
this.stare = stare;
}
public int[][] getStareNod(){
return this.stare;
}
public void setNodParinte(Nod parinte)
{
this.parinte = parinte;
}
public Nod getNodParinte() throws IOException{
return this.parinte;
}
public void setActiune(String actiune)
{
this.actiune = actiune;
}
public String getActiune()
{
return this.actiune;
}
}
Now, i create an initial node and after, a child-node from it. The problem is that when i create the child-node, the parent's 2D array becomes identical with the child's array. I tried to clone the node object but it didn't fix it. I would appreciate if someone has an ideea how to fix it and shares it.
public class test {
public static void main(String[] args) throws IOException, CloneNotSupportedException
{
int[][] p = Problema.stareInitiala();
Nod nod = new Nod(p);
Nod nodc = (Nod) nod.clone();
Nod nod1 = new Nod(nodc,"right");
Nod nod1c = (Nod) nod1.clone();
Nod nod2 = new Nod(nod1c,"up");
if (nod.getStareNod().equals(nod.getStareNod()))
System.out.print("ok");
else
System.out.print("not ok");
}
}
So if p = {{7,2,4},{5,0,6},{8,3,1}} the if statement should return "not ok", but instead i get the "ok" message.
There is nothing broken or wrong about the clone mechanism. Just a bunch or programmers who don't understand it, here's a valid one for you class.
public Object clone() {
try {
Nod clone = (Nod)super.clone();
clone.stare = (int[][])stare.clone();
return clone;
} catch (CloneNotSupportedException cnse) {
//won't happen;
throw new RuntimeError("Won't happen");
}
}
This implementation assumes that the clone wants to use the same parent as the original. If this is not the case, you will need to clone that as well, or perhaps set the parent to null.
You may want to look at the documentation of the clone() method of Object. Default clone implementation (that is if you implemented Cloneable, which you did) performs shallow copy. This is probably not what you want, you may be better off by writing your own copying method.

Categories