@Override public T get(int position) { // TODO Auto-generated method stub if (position >= counter) { System.out.println("Position mismath"); return null; } if (position == 0) { return first.get(); } if (position + 1 == counter) { return last.get(); } Node temp = first; int count = 0; while (count != position) { temp = temp.next; count++; } return temp.get(); }
/** * two nodes, my is unmodified, their is updated and has a higher version => their version is * going to be the merged version */ @Test public void nodeSimple_locallyUnmodifiedNoConflict() { Node n = new Node(new LatLon(0, 0)); n.setOsmId(1, 1); n.setModified(false); n.put("key1", "value1"); my.addPrimitive(n); Node n1 = new Node(new LatLon(0, 0)); n1.setOsmId(1, 2); n1.setModified(false); n1.put("key1", "value1-new"); n1.put("key2", "value2"); their.addPrimitive(n1); DataSetMerger visitor = new DataSetMerger(my, their); visitor.merge(); Node n2 = (Node) my.getPrimitiveById(1, OsmPrimitiveType.NODE); assertTrue(visitor.getConflicts().isEmpty()); assertSame(n, n2); // make sure the merged node is still the original node assertSame(n2.getDataSet(), my); assertEquals(1, n2.getId()); assertEquals(2, n2.getVersion()); assertFalse(n2.isModified()); assertEquals("value1-new", n2.get("key1")); assertEquals("value2", n2.get("key2")); // the merge target should not be modified assertFalse(n2.isModified()); }
private Node testAndSplit(Node s, int k, int p, char c) { if (k > p) return s == sentinel ? null : s.get(c) == null ? s : null; Edge e = s.get(str.charAt(k)); if (c == str.charAt(e.a + p - k + 1)) return null; // check if char after implicit node is c Node r = new Node(); Edge re = new Edge(e.a + p - k + 1, e.b, e.end); r.add(re); Edge se = new Edge(e.a, e.a + p - k, r); s.add(se); return r; }
public int Actual() { Node n = header.get(0); int total = 0; while (n != header) { total += n.key; n = n.get(0); } return total; }
@SuppressWarnings("unchecked") public T next() { if (nextIt == null) { nextIt = first; T cur = (T) nextIt.get(); return cur; } else { nextIt = nextIt.next; T cur = (T) nextIt.get(); return cur; } }
private Node canonize(Node s, int[] k, int p) { if (p < k[0]) return s; if (s == sentinel) { s = root; k[0]++; if (p < k[0]) return s; } Edge e = s.get(str.charAt(k[0])); while (e.b - e.a <= p - k[0]) { k[0] = k[0] + e.b - e.a + 1; s = e.end; if (k[0] <= p) e = s.get(str.charAt(k[0])); } return s; }
@Override public void remove(T t) { // TODO Auto-generated method stub if (t == null) { System.out.println("null argument remove"); return; } if (first == null) { return; } if (t == first.t) { first = first.next; return; } Node temp = first; Node tempPrev = first; while (temp != null) { if (temp.get().equals(t)) { tempPrev.next = temp.next; counter--; if (t == last.t) { last = tempPrev; } break; } tempPrev = temp; temp = temp.next; } }
/** * two identical nodes, even in id and version. No confict expected. * * <p>Can happen if data is loaded in two layers and then merged from one layer on the other. */ @Test public void nodeSimple_IdenticalNoConflict() { Node n = new Node(new LatLon(0, 0)); n.setOsmId(1, 1); n.setModified(false); n.put("key1", "value1"); my.addPrimitive(n); Node n1 = new Node(new LatLon(0, 0)); n1.setOsmId(1, 1); n1.setModified(false); n1.put("key1", "value1"); their.addPrimitive(n1); DataSetMerger visitor = new DataSetMerger(my, their); visitor.merge(); Node n2 = (Node) my.getPrimitiveById(1, OsmPrimitiveType.NODE); assertTrue(visitor.getConflicts().isEmpty()); assertNotSame(n1, n2); // make sure we have a clone assertEquals(1, n2.getId()); assertEquals(1, n2.getVersion()); assertFalse(n2.isModified()); assertEquals("value1", n2.get("key1")); // merge target not modified after merging assertFalse(n2.isModified()); }
public DirectedGraph<Node, Edge> convert() { DirectedGraph<Node, Edge> graph = new SimpleDirectedGraph<Node, Edge>(Edge.factory); // Add vertices for (Type t : hierarchy) graph.addVertex(Node.get(t)); // Add edges for (Type t : hierarchy) { for (Type i : hierarchy.getInterfaces(t)) { graph.addVertex(Node.get(i)); graph.addEdge(Node.get(t), Node.get(i)); } Type sc = hierarchy.getSuperclass(t); if (sc == null) { assert t.equals(OBJECT) : t; continue; } graph.addVertex(Node.get(sc)); graph.addEdge(Node.get(t), Node.get(sc)); } // Check for cycles if (new CycleDetector<Node, Edge>(graph).detectCycles()) throw new CyclicHierarchyException(graph.toString()); return graph; }
/** Add a tag to an empty node and test the query and get methods. */ public void testPut() { Node n = new Node(); n.put("akey", "avalue"); assertTrue(n.get("akey").equals("avalue")); assertTrue(n.getKeys().size() == 1); assertTrue(n.keySet().size() == 1); assertTrue(n.keySet().contains("akey")); }
private <T, U> Node buildPathToNode(final Class<U> clazz) throws ClassHierarchyException { final String[] path = clazz.getName().split("\\$"); Node root = namespace; for (int i = 0; i < path.length - 1; i++) { root = root.get(path[i]); } if (root == null) { throw new NullPointerException(); } final Node parent = root; final Type argType = ReflectionUtilities.getNamedParameterTargetOrNull(clazz); if (argType == null) { return JavaNodeFactory.createClassNode(parent, clazz); } else { // checked inside of NamedParameterNode, using reflection. @SuppressWarnings("unchecked") final NamedParameterNode<T> np = JavaNodeFactory.createNamedParameterNode( parent, (Class<? extends Name<T>>) clazz, argType); if (parameterParser.canParse(ReflectionUtilities.getFullName(argType)) && clazz.getAnnotation(NamedParameter.class).default_class() != Void.class) { throw new ClassHierarchyException( "Named parameter " + ReflectionUtilities.getFullName(clazz) + " defines default implementation for parsable type " + ReflectionUtilities.getFullName(argType)); } final String shortName = np.getShortName(); if (shortName != null) { final NamedParameterNode<?> oldNode = shortNames.get(shortName); if (oldNode != null) { if (oldNode.getFullName().equals(np.getFullName())) { throw new IllegalStateException( "Tried to double bind " + oldNode.getFullName() + " to short name " + shortName); } throw new ClassHierarchyException( "Named parameters " + oldNode.getFullName() + " and " + np.getFullName() + " have the same short name: " + shortName); } shortNames.put(shortName, np); } return np; } }
/** Add two tags to an empty node and test the query and get methods. */ public void testPut2() { Node n = new Node(); n.put("key.1", "value.1"); n.put("key.2", "value.2"); assertTrue(n.get("key.1").equals("value.1")); assertTrue(n.get("key.2").equals("value.2")); assertTrue(n.getKeys().size() == 2); assertTrue(n.hasKeys()); assertTrue(n.hasKey("key.1")); assertTrue(n.hasKey("key.2")); assertTrue(!n.hasKey("nosuchkey")); }
public V get(double key) { for (; ; ) { Node b = skipListMap.findGrandPredecessor(key, refCntCallback); if (b == null) b = head; if (b != head && b.first() <= key && key <= b.last()) { // can happen if there's inconsistency between skipListMap and orderedMap(this). // The inconsistency can happen from Node.putAtomicReally(), // where it adds to, and removes from skiplistMap. Object val = b.get(key); release(b); if (val == Retry) continue; return (V) val; } assert b == head || b.last() < key; Node n = safeNext(b); release(b); if (n == null) { return null; } else if (n.first() <= key && key <= n.last()) { Object val = n.get(key); release(n); if (val == Retry) continue; return (V) val; } Node f = safeNext(n); release(n); if (f == null) { return null; } else { if (isInconsistentNextNode(f, key)) { release(f); continue; } Object val = f.get(key); release(f); if (val == Retry) continue; return (V) val; } } }
/** Remove tags from a node with two tags and test the state of the node. */ public void testRemove() { Node n = new Node(); n.put("key.1", "value.1"); n.put("key.2", "value.2"); n.remove("nosuchkey"); // should work assertTrue(n.getKeys().size() == 2); // still 2 tags ? n.remove("key.1"); assertTrue(n.getKeys().size() == 1); assertTrue(!n.hasKey("key.1")); assertTrue(n.get("key.1") == null); assertTrue(n.hasKey("key.2")); assertTrue(n.get("key.2").equals("value.2")); n.remove("key.2"); assertTrue(n.getKeys().size() == 0); assertTrue(!n.hasKey("key.1")); assertTrue(n.get("key.1") == null); assertTrue(!n.hasKey("key.2")); assertTrue(n.get("key.2") == null); }
@Override public boolean contains(T t) { // TODO Auto-generated method stub Node temp = first; boolean cont = false; while (temp != null) { if (temp.get().equals(t)) { cont = true; break; } temp = temp.next; } return cont; }
private Node getAlreadyBoundNode(final String name) throws NameResolutionException { Node root = namespace; final String[] toks = name.split("\\$"); final String outerClassName = toks[0]; root = root.get(outerClassName); if (root == null) { throw new NameResolutionException(name, outerClassName); } for (int i = 1; i < toks.length; i++) { root = root.get(toks[i]); if (root == null) { final StringBuilder sb = new StringBuilder(outerClassName); for (int j = 0; j < i; j++) { sb.append(toks[j]); if (j != i - 1) { sb.append("."); } } throw new NameResolutionException(name, sb.toString()); } } return root; }
/** * Find the integer value associated with this key, or nothing if this key is not in the keep. * * @param key An object. * @return An integer */ public int match(Kim kim, int from, int thru) { Node node = this.root; int best = none; for (int at = from; at < thru; at += 1) { node = node.get(kim.get(at)); if (node == null) { break; } if (node.integer != none) { best = node.integer; } from += 1; } return best; }
public void remove() { if (nextIt == null) { return; } if (nextIt.t == null) { System.out.println("null argument remove"); return; } if (first == null) { return; } if (nextIt == first) { first = first.next; return; } Node temp = first; Node tempPrev = null; while (temp != null) { if (temp.get().equals(nextIt.t)) { if (tempPrev != null) { tempPrev.next = temp.next; } if (nextIt == last) { last = tempPrev; } // nextIt = temp.next; counter--; break; } tempPrev = temp; temp = temp.next; } }
/** * their node has no assigned id (id == 0) and is semantically equal to one of my nodes with id == * 0 * * <p>=> merge it onto my node. */ @Test public void nodeSimple_NoIdSemanticallyEqual() { Calendar cal = GregorianCalendar.getInstance(); User myUser = User.createOsmUser(1111, "my"); User theirUser = User.createOsmUser(222, "their"); Node n = new Node(); n.setCoor(new LatLon(0, 0)); n.put("key1", "value1"); n.setUser(myUser); n.setTimestamp(cal.getTime()); my.addPrimitive(n); Node n1 = new Node(); n1.setCoor(new LatLon(0, 0)); n1.put("key1", "value1"); cal.add(Calendar.HOUR, 1); Date timestamp = cal.getTime(); n1.setTimestamp(timestamp); n1.setUser(theirUser); their.addPrimitive(n1); DataSetMerger visitor = new DataSetMerger(my, their); visitor.merge(); Node n2 = my.getNodes().iterator().next(); assertEquals(0, visitor.getConflicts().size()); assertEquals("value1", n2.get("key1")); assertEquals(n1.getRawTimestamp(), n2.getRawTimestamp()); assertEquals(theirUser, n2.getUser()); assertSame(n2, n); assertNotSame(n2, n1); assertSame(n2.getDataSet(), my); }
/** * my node is incomplete, their node is complete * * <p>=> merge it onto my node. My node becomes complete */ @Test public void nodeSimple_IncompleteNode() { Node n = new Node(1); my.addPrimitive(n); Node n1 = new Node(); n1.setCoor(new LatLon(0, 0)); n1.setOsmId(1, 1); n1.put("key1", "value1"); Date timestamp = new Date(); n1.setTimestamp(timestamp); their.addPrimitive(n1); DataSetMerger visitor = new DataSetMerger(my, their); visitor.merge(); Node n2 = my.getNodes().iterator().next(); assertEquals(0, visitor.getConflicts().size()); assertEquals("value1", n2.get("key1")); assertEquals(n1.getRawTimestamp(), n2.getRawTimestamp()); assertFalse(n2.isIncomplete()); assertSame(n2, n); }
@Override public Value get(Key key) { Node node = findNodeFor(key); return node.get(key); }
@SuppressWarnings("unchecked") public V get(Object key) { return (V) root.get(comparator, key); }