public void testMergeWithNoOtherChanges() throws Exception { remove.setChild(ssa); remove.mergeInto(delta, NOW); assertEquals("Remove should have been added", 1, delta.getChanges().size()); assertSame(remove, delta.getChanges().get(0)); assertEquals("Updated date not set", NOW, remove.getUpdatedDate()); }
public void testMergeWithEquivalentAddPresent() throws Exception { delta.addChange(Add.create(ssc, 2)); remove.setChild(ssc); remove.mergeInto(delta, NOW); assertEquals("Remove should have canceled add", 0, delta.getChanges().size()); }
public void testMergeWithReorderAloneDoesNotCancelReorder() throws Exception { delta.addChange(Reorder.create(ssa, 0, 1)); remove.setChild(ssa); remove.mergeInto(delta, NOW); assertEquals("Remove should not have canceled anything", 2, delta.getChanges().size()); }
public void testMergeWithAddFollowedByReorderCancelsBoth() throws Exception { delta.addChange(Add.create(ssc, 2)); Reorder.create(ssc, 2, 1).mergeInto(delta, NOW); remove.setChild(ssc); remove.mergeInto(delta, NOW); assertEquals("Remove should have canceled add and reorder", 0, delta.getChanges().size()); }
/** * Restore the text to original. Opposite to applyTo() method. * * @param target the given target * @return the restored text */ public List<T> restore(final List<T> target) { final List<T> result = new LinkedList<T>(target); final ListIterator<Delta<T>> it = getDeltas().listIterator(deltaSize); while (it.hasPrevious()) { final Delta<T> delta = it.previous(); delta.restore(result); } return result; }
/** * Apply this patch to the given target * * @return the patched text * @throws PatchFailedException if can't apply patch */ public List<T> applyTo(final List<T> target) throws PatchFailedException { final List<T> result = new LinkedList<T>(target); final ListIterator<Delta<T>> it = getDeltas().listIterator(deltaSize); while (it.hasPrevious()) { final Delta<T> delta = it.previous(); delta.applyTo(result); } return result; }
public void testMergeWithEquivalentRemovePresent() throws Exception { Remove expectedRemove = Remove.create(ssb); delta.addChange(expectedRemove); remove.setChild(ssb); remove.mergeInto(delta, NOW); assertEquals("Duplicate remove should not have been added", 1, delta.getChanges().size()); assertEquals(expectedRemove, delta.getChanges().get(0)); }
public void testFindMatchingDelta() throws Exception { Amendment a1 = Fixtures.createAmendment("Amendment", DateTools.createDate(2007, Calendar.APRIL, 6)); Epoch epoch1 = new Epoch(); epoch1.setGridId("epoch1"); Delta<Epoch> delta1 = Delta.createDeltaFor(epoch1); delta1.setGridId("delta1"); a1.addDelta(delta1); Delta actualDelta = a1.getMatchingDelta("delta1", "epoch1", delta1.getClass()); assertNotNull("Delta not found", actualDelta); }
/** * Test. * * @param v1 the v1 * @param v2 the v2 * @throws IOException Signals that an I/O exception has occurred. */ public void test(String v1, String v2) throws IOException { CharSequence string = Delta.toString(forFile(v1)); CharSequence string2 = Delta.toString(forFile(v2)); Delta d = new Delta(); String delta = d.compute(string, string2); // System.err.println(delta); // System.err.println("----"); String string3 = new TextPatcher(string).patch(delta); // System.out.println(string3); assertEquals(string2.toString(), string3); }
public void run() throws IOException { // Update controllers. // This may invalidate the file system object, so it must be // done first in case srcController and dstController are the // same! class SrcControllerUpdater implements IORunnable { public void run() throws IOException { srcController.autoUmount(srcEntryName); srcController.readLock().lock(); // downgrade to read lock upon return } } // class SrcControllerUpdater final ArchiveEntry srcEntry, dstEntry; final Delta delta; srcController.runWriteLocked(new SrcControllerUpdater()); try { dstController.autoUmount(dstEntryName); // Get source archive entry. final ArchiveFileSystem srcFileSystem = srcController.autoMount(false); srcEntry = srcFileSystem.get(srcEntryName); // Get destination archive entry. final boolean lenient = File.isLenient(); final ArchiveFileSystem dstFileSystem = dstController.autoMount(lenient); delta = dstFileSystem.link(dstEntryName, lenient, preserve ? srcEntry : null); dstEntry = delta.getEntry(); // Create input stream. in = srcController.createInputStream(srcEntry, dstEntry); } finally { srcController.readLock().unlock(); } try { // Create output stream. out = dstController.createOutputStream(dstEntry, srcEntry); try { // Now link the destination entry into the file system. delta.commit(); } catch (IOException ex) { out.close(); throw ex; } } catch (IOException ex) { try { in.close(); } catch (IOException inFailure) { throw new InputIOException(inFailure); } throw ex; } }
public void testDeepEqualsWhenDeltaAreNotEquals() throws Exception { Amendment a1 = Fixtures.createAmendment("Amendment", DateTools.createDate(2007, Calendar.APRIL, 6)); Amendment a2 = Fixtures.createAmendment("Amendment", DateTools.createDate(2007, Calendar.APRIL, 6)); Epoch epoch1 = new Epoch(); epoch1.setName("Epoch"); epoch1.setGridId("GridId1"); Delta<Epoch> delta1 = Delta.createDeltaFor(epoch1); delta1.setGridId("delta1"); delta1.addChanges(PropertyChange.create("name", "A", "C"), Add.create(new StudySegment())); a1.addDelta(delta1); Epoch epoch2 = new Epoch(); epoch2.setName("Epoch"); epoch2.setGridId("GridId1"); Delta<Epoch> delta2 = Delta.createDeltaFor(epoch2); delta2.setGridId("delta1"); delta2.addChanges(PropertyChange.create("name", "A", "B"), Add.create(new StudySegment())); a2.addDelta(delta2); Differences differences = a1.deepEquals(a2); assertFalse(differences.getChildDifferences().isEmpty()); assertChildDifferences( differences, new String[] {"delta for epoch GridId1", "property change for name"}, "new value \"C\" does not match \"B\""); }
public void testSetMemOnlyRecursiveToDeltas() throws Exception { Amendment amendment = Fixtures.createAmendment(null, DateTools.createDate(2008, Calendar.JUNE, 22)); amendment.addDelta(Delta.createDeltaFor(new Study())); amendment.setMemoryOnly(true); assertTrue(amendment.getDeltas().get(0).isMemoryOnly()); }
public void testUpdatedDateIsMaxAcrossDeltas() throws Exception { a3.addDelta(Delta.createDeltaFor(new Epoch(), PropertyChange.create("name", "A", "B"))); a3.addDelta(Delta.createDeltaFor(new Epoch(), Add.create(new StudySegment()))); a3.getDeltas() .get(0) .getChanges() .get(0) .setUpdatedDate(DateTools.createDate(2005, Calendar.MAY, 3)); a3.getDeltas() .get(1) .getChanges() .get(0) .setUpdatedDate(DateTools.createDate(2005, Calendar.MAY, 4)); assertDayOfDate(2005, Calendar.MAY, 4, a3.getUpdatedDate()); }
public void testCloneDeepClonesDeltas() throws Exception { Amendment src = Fixtures.createAmendment(null, DateTools.createDate(2008, Calendar.JUNE, 22)); src.addDelta(Delta.createDeltaFor(new Study())); Amendment clone = src.clone(); assertEquals("Wrong number of cloned changes", 1, clone.getDeltas().size()); assertNotSame("Deltas not deep cloned", src.getDeltas().get(0), clone.getDeltas().get(0)); }
@Override protected void setUp() throws Exception { super.setUp(); remove = new Remove(); epoch = setId(5, Epoch.create("Treatment", "A", "B", "C")); ssa = setId(1, epoch.getStudySegments().get(0)); ssb = setId(2, epoch.getStudySegments().get(1)); ssc = setId(3, epoch.getStudySegments().get(2)); epoch.getStudySegments().remove(ssc); delta = Delta.createDeltaFor(epoch); }
private List<Game> convertToGames(Cursor cursor) { List<Game> result = new ArrayList<Game>(); Game currentGame = null; while (cursor.moveToNext()) { int currentId = cursor.getInt(0); if (currentGame == null || currentGame.getId() != currentId) { // new // Game currentGame = new Game(); currentGame.setId(currentId); currentGame.setDateStarted(cursor.getLong(1)); currentGame.setDateSaved(cursor.getLong(2)); currentGame.setName(cursor.getString(3)); result.add(currentGame); } List<PlayerScore> playerScores = new ArrayList<PlayerScore>(); // build up all the PlayerScores do { if (cursor.getInt(0) != currentId) { cursor.moveToPrevious(); // went too far break; } PlayerScore playerScore = new PlayerScore(); playerScore.setId(cursor.getInt(4)); playerScore.setName(cursor.getString(5)); playerScore.setScore(cursor.getLong(6)); playerScore.setPlayerNumber(cursor.getInt(7)); playerScore.setHistory( Delta.fromJoinedStrings( StringUtil.nullToEmpty(cursor.getString(8)), StringUtil.nullToEmpty(cursor.getString(9)))); playerScore.setLastUpdate(cursor.getLong(10)); playerScore.setPlayerColor(PlayerColor.deserialize(cursor.getString(11))); playerScores.add(playerScore); } while (cursor.moveToNext()); Collections.sort(playerScores, PlayerScore.sortByPlayerNumber()); currentGame.setPlayerScores(playerScores); } return result; }
public void testDeepEqualsWhenNoOfDeltaAreDifferent() throws Exception { Amendment a1 = Fixtures.createAmendment("Amendment", DateTools.createDate(2007, Calendar.APRIL, 6)); Amendment a2 = Fixtures.createAmendment("Amendment", DateTools.createDate(2007, Calendar.APRIL, 6)); a1.addDelta( Delta.createDeltaFor( new Epoch(), PropertyChange.create("name", "A", "B"), Add.create(new StudySegment()))); a2.addDelta( Delta.createDeltaFor( new Epoch(), PropertyChange.create("name", "A", "B"), Add.create(new StudySegment()))); a2.addDelta( Delta.createDeltaFor( new Epoch(), PropertyChange.create("name", "A", "B"), Add.create(new StudySegment()))); Differences differences = a1.deepEquals(a2); assertFalse(differences.getMessages().isEmpty()); assertEquals( "Amendment are Equals", "number of deltas does not match: 1 != 2", differences.getMessages().get(0)); }
public void commit(final FeatureStore store) throws DataStoreException { writeLock.lock(); try { for (int i = 0, n = deltas.size(); i < n; i++) { final Delta alt = deltas.get(i); final Map<String, String> updates = alt.commit(store); alt.dispose(); // update next deltas if (updates != null) { for (int j = i + 1; j < n; j++) { final Delta next = deltas.get(j); next.update(updates); } } } deltas.clear(); readCopy = null; } finally { writeLock.unlock(); } }
public void run() throws IOException { // Update controller. // This may invalidate the file system object, so it must be // done first in case srcController and dstController are the // same! dstController.autoUmount(dstEntryName); final boolean lenient = File.isLenient(); // Get source archive entry. final ArchiveEntry srcEntry = new RfsEntry(src); // Get destination archive entry. final ArchiveFileSystem dstFileSystem = dstController.autoMount(lenient); final Delta delta = dstFileSystem.link(dstEntryName, lenient, preserve ? srcEntry : null); final ArchiveEntry dstEntry = delta.getEntry(); // Create output stream. out = dstController.createOutputStream(dstEntry, srcEntry); // Now link the destination entry into the file system. delta.commit(); }
private void savePlayerScores(int gameId, List<PlayerScore> playerScores) { synchronized (GameDBHelper.class) { int newId = -1; for (PlayerScore playerScore : playerScores) { Pair<String, String> historyAsStrings = Delta.toJoinedStrings(playerScore.getHistory()); if (playerScore.getId() != -1) { // already exists; update updatePlayerScore( playerScore.getId(), playerScore.getName(), playerScore.getScore(), playerScore.getPlayerNumber(), historyAsStrings.getFirst(), historyAsStrings.getSecond(), playerScore.getLastUpdate(), PlayerColor.serialize(playerScore.getPlayerColor())); } else { // else insert new rows in the table if (newId == -1) { newId = getMaxPlayerScoreId() + 1; } else { newId++; } ContentValues values = new ContentValues(); values.put(COLUMN_ID, newId); values.put(COLUMN_GAME_ID, gameId); values.put(COLUMN_HISTORY, historyAsStrings.getFirst()); values.put(COLUMN_HISTORY_TIMESTAMPS, historyAsStrings.getSecond()); values.put(COLUMN_NAME, playerScore.getName()); values.put(COLUMN_PLAYER_NUMBER, playerScore.getPlayerNumber()); values.put(COLUMN_SCORE, playerScore.getScore()); values.put(COLUMN_COLOR, PlayerColor.serialize(playerScore.getPlayerColor())); values.put(COLUMN_LAST_UPDATE, playerScore.getLastUpdate()); db.insert(TABLE_PLAYER_SCORES, null, values); // set the new id on the PlayerScore playerScore.setId(newId); log.d("new playerScore id is %s", newId); } } } }
public void testDeepEqualsWithMismatchedChanges() throws Exception { Amendment a = new Amendment(); a.getDeltas() .add( setGridId( "D1", Delta.createDeltaFor( setGridId("PC1", new PlannedCalendar()), Add.create(setGridId("E1", new Epoch()))))); Amendment b = new Amendment(); b.getDeltas() .add( setGridId( "D1", Delta.createDeltaFor( setGridId("PC1", new PlannedCalendar()), Remove.create(setGridId("E1", new Epoch()))))); assertChildDifferences( a.deepEquals(b), new String[] {"delta for planned calendar PC1"}, "add of epoch:E1 replaced by remove of epoch:E1"); }
public void testDeepEqualsWhenNoDeltaFoundInAmendment() throws Exception { Amendment a1 = Fixtures.createAmendment("Amendment", DateTools.createDate(2007, Calendar.APRIL, 6)); Amendment a2 = Fixtures.createAmendment("Amendment", DateTools.createDate(2007, Calendar.APRIL, 6)); Epoch epoch1 = new Epoch(); epoch1.setGridId("epoch1"); Delta<Epoch> delta1 = Delta.createDeltaFor(epoch1); delta1.setGridId("delta1"); delta1.addChanges(PropertyChange.create("name", "A", "B"), Add.create(new StudySegment())); a1.addDelta(delta1); StudySegment segment1 = new StudySegment(); segment1.setGridId("segment1"); Delta<StudySegment> delta2 = Delta.createDeltaFor(segment1); delta2.setGridId("delta1"); delta2.addChanges(PropertyChange.create("name", "A", "B"), Add.create(new StudySegment())); a2.addDelta(delta2); Differences differences = a1.deepEquals(a2); assertFalse(differences.getMessages().isEmpty()); assertEquals( "Amendments are equals", "no delta for epoch epoch1 found", differences.getMessages().get(0)); }
public void testMergeTwoIntoDeltaWithIndexesThenRemoveOne() throws Exception { StudySegment retained0 = setId(17, new StudySegment()); StudySegment retained1 = setId(18, new StudySegment()); StudySegment retained2 = setId(19, new StudySegment()); delta.addChange(Add.create(retained0, 2)); delta.addChange(Add.create(ssc, 3)); delta.addChange(Add.create(retained1, 4)); delta.addChange(Add.create(retained2, 5)); remove.setChild(ssc); remove.mergeInto(delta, NOW); assertEquals("Wrong number of changes in delta", 3, delta.getChanges().size()); Add add0 = (Add) delta.getChanges().get(0); Add add1 = (Add) delta.getChanges().get(1); Add add2 = (Add) delta.getChanges().get(2); assertAdd("Index for earlier retained add updated", retained0, 2, add0); assertChangeTime("Unchanged add time incorrectly updated", null, add0); assertAdd("Index for later retained add not updated", retained1, 3, add1); assertChangeTime("Updated add time not updated", NOW, add1); assertAdd("Index for later retained add not updated", retained2, 4, add2); assertChangeTime("Updated add time not updated", NOW, add2); }
public void testMergeWithChildNotInNode() throws Exception { remove.setChild(ssc); remove.mergeInto(delta, NOW); assertEquals("Remove should not have been added", 0, delta.getChanges().size()); }
@Override public int compare(Delta<?> a, Delta<?> b) { return a.getOriginal().getStartPosition() - b.getOriginal().getStartPosition(); }
private void ensureUpdatedDate(Amendment expectedAmendment, Date expectedDate) { expectedAmendment.getDeltas().clear(); Delta<?> delta = Delta.createDeltaFor(new Epoch(), PropertyChange.create("name", "A", "B")); delta.getChanges().get(0).setUpdatedDate(expectedDate); expectedAmendment.addDelta(delta); }
/** * Main method just instanciates a delta object and calls run. * * @param args Not used */ public static void main(String args[]) { Delta delta = new Delta(); delta.run(); }
public static void main(String[] args) { // instantiate some sequences Constant c1 = new Constant(4, 11); Constant c2 = new Constant(4, 0); Constant c3 = new Constant(0, 11); Constant c4 = new Constant(0, 0); Constant c5 = new Constant(4, 88); Constant c6 = new Constant(3, 55); Constant c7 = new Constant(0, -77); Constant c8 = new Constant(10, 22); Delta d1 = new Delta(4, 1, 1); Delta d2 = new Delta(4, 1, -1); Delta d3 = new Delta(0, 1, -1); Delta d4 = new Delta(0, 1, 0); Delta d5 = new Delta(4, 1, 0); Delta d6 = new Delta(3, 21, 1); Delta d7 = new Delta(0, 25, -2); Delta d8 = new Delta(10, 2, 4); Jumble j1 = new Jumble(new int[] {}); Jumble j2 = new Jumble(new int[] {88}); Jumble j3 = new Jumble(new int[] {97, 55}); Jumble j4 = new Jumble(new int[] {199, 198, 197, 196, 195, 194, 193, 192}); Jumble j5 = new Jumble(new int[] {102, 103, 101, 107, 109}); Jumble j6 = new Jumble(new int[] {41, 42, 43, 44}); // do everything twice just to double check for (int i = 0; i < 2; i++) { System.out.print("c1" + ":"); System.out.print(c1); System.out.println(":"); System.out.println( "(Seq)c1 instanceof Constant" + ":" + ((Seq) c1 instanceof Constant) + ":"); System.out.println("(Seq)c1 instanceof Delta" + ":" + ((Seq) c1 instanceof Delta) + ":"); System.out.println("(Seq)c1 instanceof Jumble" + ":" + ((Seq) c1 instanceof Jumble) + ":"); System.out.println("c1.min()" + ":" + (c1.min()) + ":"); System.out.print("c2" + ":"); System.out.print(c2); System.out.println(":"); System.out.println( "(Seq)c2 instanceof Constant" + ":" + ((Seq) c2 instanceof Constant) + ":"); System.out.println("(Seq)c2 instanceof Delta" + ":" + ((Seq) c2 instanceof Delta) + ":"); System.out.println("(Seq)c2 instanceof Jumble" + ":" + ((Seq) c2 instanceof Jumble) + ":"); System.out.println("c2.min()" + ":" + (c2.min()) + ":"); System.out.print("c3" + ":"); System.out.print(c3); System.out.println(":"); System.out.println( "(Seq)c3 instanceof Constant" + ":" + ((Seq) c3 instanceof Constant) + ":"); System.out.println("(Seq)c3 instanceof Delta" + ":" + ((Seq) c3 instanceof Delta) + ":"); System.out.println("(Seq)c3 instanceof Jumble" + ":" + ((Seq) c3 instanceof Jumble) + ":"); System.out.println("c3.min()" + ":" + (c3.min()) + ":"); System.out.print("c4" + ":"); System.out.print(c4); System.out.println(":"); System.out.println( "(Seq)c4 instanceof Constant" + ":" + ((Seq) c4 instanceof Constant) + ":"); System.out.println("(Seq)c4 instanceof Delta" + ":" + ((Seq) c4 instanceof Delta) + ":"); System.out.println("(Seq)c4 instanceof Jumble" + ":" + ((Seq) c4 instanceof Jumble) + ":"); System.out.println("c4.min()" + ":" + (c4.min()) + ":"); System.out.print("c5" + ":"); System.out.print(c5); System.out.println(":"); System.out.println( "(Seq)c5 instanceof Constant" + ":" + ((Seq) c5 instanceof Constant) + ":"); System.out.println("(Seq)c5 instanceof Delta" + ":" + ((Seq) c5 instanceof Delta) + ":"); System.out.println("(Seq)c5 instanceof Jumble" + ":" + ((Seq) c5 instanceof Jumble) + ":"); System.out.println("c5.min()" + ":" + (c5.min()) + ":"); System.out.print("c6" + ":"); System.out.print(c6); System.out.println(":"); System.out.println( "(Seq)c6 instanceof Constant" + ":" + ((Seq) c6 instanceof Constant) + ":"); System.out.println("(Seq)c6 instanceof Delta" + ":" + ((Seq) c6 instanceof Delta) + ":"); System.out.println("(Seq)c6 instanceof Jumble" + ":" + ((Seq) c6 instanceof Jumble) + ":"); System.out.println("c6.min()" + ":" + (c6.min()) + ":"); System.out.print("c7" + ":"); System.out.print(c7); System.out.println(":"); System.out.println( "(Seq)c7 instanceof Constant" + ":" + ((Seq) c7 instanceof Constant) + ":"); System.out.println("(Seq)c7 instanceof Delta" + ":" + ((Seq) c7 instanceof Delta) + ":"); System.out.println("(Seq)c7 instanceof Jumble" + ":" + ((Seq) c7 instanceof Jumble) + ":"); System.out.println("c7.min()" + ":" + (c7.min()) + ":"); System.out.print("c8" + ":"); System.out.print(c8); System.out.println(":"); System.out.println( "(Seq)c8 instanceof Constant" + ":" + ((Seq) c8 instanceof Constant) + ":"); System.out.println("(Seq)c8 instanceof Delta" + ":" + ((Seq) c8 instanceof Delta) + ":"); System.out.println("(Seq)c8 instanceof Jumble" + ":" + ((Seq) c8 instanceof Jumble) + ":"); System.out.println("c8.min()" + ":" + (c8.min()) + ":"); System.out.print("d1" + ":"); System.out.print(d1); System.out.println(":"); System.out.println( "(Seq)d1 instanceof Constant" + ":" + ((Seq) d1 instanceof Constant) + ":"); System.out.println("(Seq)d1 instanceof Delta" + ":" + ((Seq) d1 instanceof Delta) + ":"); System.out.println("(Seq)d1 instanceof Jumble" + ":" + ((Seq) d1 instanceof Jumble) + ":"); System.out.println("d1.min()" + ":" + (d1.min()) + ":"); System.out.print("d2" + ":"); System.out.print(d2); System.out.println(":"); System.out.println( "(Seq)d2 instanceof Constant" + ":" + ((Seq) d2 instanceof Constant) + ":"); System.out.println("(Seq)d2 instanceof Delta" + ":" + ((Seq) d2 instanceof Delta) + ":"); System.out.println("(Seq)d2 instanceof Jumble" + ":" + ((Seq) d2 instanceof Jumble) + ":"); System.out.println("d2.min()" + ":" + (d2.min()) + ":"); System.out.print("d3" + ":"); System.out.print(d3); System.out.println(":"); System.out.println( "(Seq)d3 instanceof Constant" + ":" + ((Seq) d3 instanceof Constant) + ":"); System.out.println("(Seq)d3 instanceof Delta" + ":" + ((Seq) d3 instanceof Delta) + ":"); System.out.println("(Seq)d3 instanceof Jumble" + ":" + ((Seq) d3 instanceof Jumble) + ":"); System.out.println("d3.min()" + ":" + (d3.min()) + ":"); System.out.print("d4" + ":"); System.out.print(d4); System.out.println(":"); System.out.println( "(Seq)d4 instanceof Constant" + ":" + ((Seq) d4 instanceof Constant) + ":"); System.out.println("(Seq)d4 instanceof Delta" + ":" + ((Seq) d4 instanceof Delta) + ":"); System.out.println("(Seq)d4 instanceof Jumble" + ":" + ((Seq) d4 instanceof Jumble) + ":"); System.out.println("d4.min()" + ":" + (d4.min()) + ":"); System.out.print("d5" + ":"); System.out.print(d5); System.out.println(":"); System.out.println( "(Seq)d5 instanceof Constant" + ":" + ((Seq) d5 instanceof Constant) + ":"); System.out.println("(Seq)d5 instanceof Delta" + ":" + ((Seq) d5 instanceof Delta) + ":"); System.out.println("(Seq)d5 instanceof Jumble" + ":" + ((Seq) d5 instanceof Jumble) + ":"); System.out.println("d5.min()" + ":" + (d5.min()) + ":"); System.out.print("d6" + ":"); System.out.print(d6); System.out.println(":"); System.out.println( "(Seq)d6 instanceof Constant" + ":" + ((Seq) d6 instanceof Constant) + ":"); System.out.println("(Seq)d6 instanceof Delta" + ":" + ((Seq) d6 instanceof Delta) + ":"); System.out.println("(Seq)d6 instanceof Jumble" + ":" + ((Seq) d6 instanceof Jumble) + ":"); System.out.println("d6.min()" + ":" + (d6.min()) + ":"); System.out.print("d6" + ":"); System.out.print(d6); System.out.println(":"); System.out.println( "(Seq)d6 instanceof Constant" + ":" + ((Seq) d6 instanceof Constant) + ":"); System.out.println("(Seq)d6 instanceof Delta" + ":" + ((Seq) d6 instanceof Delta) + ":"); System.out.println("(Seq)d6 instanceof Jumble" + ":" + ((Seq) d6 instanceof Jumble) + ":"); System.out.println("d6.min()" + ":" + (d6.min()) + ":"); System.out.print("d7" + ":"); System.out.print(d7); System.out.println(":"); System.out.println( "(Seq)d7 instanceof Constant" + ":" + ((Seq) d7 instanceof Constant) + ":"); System.out.println("(Seq)d7 instanceof Delta" + ":" + ((Seq) d7 instanceof Delta) + ":"); System.out.println("(Seq)d7 instanceof Jumble" + ":" + ((Seq) d7 instanceof Jumble) + ":"); System.out.println("d7.min()" + ":" + (d7.min()) + ":"); System.out.print("d8" + ":"); System.out.print(d8); System.out.println(":"); System.out.println( "(Seq)d8 instanceof Constant" + ":" + ((Seq) d8 instanceof Constant) + ":"); System.out.println("(Seq)d8 instanceof Delta" + ":" + ((Seq) d8 instanceof Delta) + ":"); System.out.println("(Seq)d8 instanceof Jumble" + ":" + ((Seq) d8 instanceof Jumble) + ":"); System.out.println("d8.min()" + ":" + (d8.min()) + ":"); System.out.print("j1" + ":"); System.out.print(j1); System.out.println(":"); System.out.println( "(Seq)j1 instanceof Constant" + ":" + ((Seq) j1 instanceof Constant) + ":"); System.out.println("(Seq)j1 instanceof Delta" + ":" + ((Seq) j1 instanceof Delta) + ":"); System.out.println("(Seq)j1 instanceof Jumble" + ":" + ((Seq) j1 instanceof Jumble) + ":"); System.out.println("j1.min()" + ":" + (j1.min()) + ":"); System.out.print("j2" + ":"); System.out.print(j2); System.out.println(":"); System.out.println( "(Seq)j2 instanceof Constant" + ":" + ((Seq) j2 instanceof Constant) + ":"); System.out.println("(Seq)j2 instanceof Delta" + ":" + ((Seq) j2 instanceof Delta) + ":"); System.out.println("(Seq)j2 instanceof Jumble" + ":" + ((Seq) j2 instanceof Jumble) + ":"); System.out.println("j2.min()" + ":" + (j2.min()) + ":"); System.out.print("j3" + ":"); System.out.print(j3); System.out.println(":"); System.out.println( "(Seq)j3 instanceof Constant" + ":" + ((Seq) j3 instanceof Constant) + ":"); System.out.println("(Seq)j3 instanceof Delta" + ":" + ((Seq) j3 instanceof Delta) + ":"); System.out.println("(Seq)j3 instanceof Jumble" + ":" + ((Seq) j3 instanceof Jumble) + ":"); System.out.println("j3.min()" + ":" + (j3.min()) + ":"); System.out.print("j4" + ":"); System.out.print(j4); System.out.println(":"); System.out.println( "(Seq)j4 instanceof Constant" + ":" + ((Seq) j4 instanceof Constant) + ":"); System.out.println("(Seq)j4 instanceof Delta" + ":" + ((Seq) j4 instanceof Delta) + ":"); System.out.println("(Seq)j4 instanceof Jumble" + ":" + ((Seq) j4 instanceof Jumble) + ":"); System.out.println("j4.min()" + ":" + (j4.min()) + ":"); System.out.print("j5" + ":"); System.out.print(j5); System.out.println(":"); System.out.println( "(Seq)j5 instanceof Constant" + ":" + ((Seq) j5 instanceof Constant) + ":"); System.out.println("(Seq)j5 instanceof Delta" + ":" + ((Seq) j5 instanceof Delta) + ":"); System.out.println("(Seq)j5 instanceof Jumble" + ":" + ((Seq) j5 instanceof Jumble) + ":"); System.out.println("j5.min()" + ":" + (j5.min()) + ":"); System.out.print("j6" + ":"); System.out.print(j6); System.out.println(":"); System.out.println( "(Seq)j6 instanceof Constant" + ":" + ((Seq) j6 instanceof Constant) + ":"); System.out.println("(Seq)j6 instanceof Delta" + ":" + ((Seq) j6 instanceof Delta) + ":"); System.out.println("(Seq)j6 instanceof Jumble" + ":" + ((Seq) j6 instanceof Jumble) + ":"); System.out.println("j6.min()" + ":" + (j6.min()) + ":"); // check a bit more Seq x; x = j3; System.out.print("x" + ":"); System.out.print(x); System.out.println(":"); System.out.println("(Seq)x instanceof Constant" + ":" + ((Seq) x instanceof Constant) + ":"); System.out.println("(Seq)x instanceof Delta" + ":" + ((Seq) x instanceof Delta) + ":"); System.out.println("(Seq)x instanceof Jumble" + ":" + ((Seq) x instanceof Jumble) + ":"); System.out.println("x.min()" + ":" + (x.min()) + ":"); x = d4; System.out.print("x" + ":"); System.out.print(x); System.out.println(":"); System.out.println("(Seq)x instanceof Constant" + ":" + ((Seq) x instanceof Constant) + ":"); System.out.println("(Seq)x instanceof Delta" + ":" + ((Seq) x instanceof Delta) + ":"); System.out.println("(Seq)x instanceof Jumble" + ":" + ((Seq) x instanceof Jumble) + ":"); System.out.println("x.min()" + ":" + (x.min()) + ":"); x = c1; System.out.print("x" + ":"); System.out.print(x); System.out.println(":"); System.out.println("(Seq)x instanceof Constant" + ":" + ((Seq) x instanceof Constant) + ":"); System.out.println("(Seq)x instanceof Delta" + ":" + ((Seq) x instanceof Delta) + ":"); System.out.println("(Seq)x instanceof Jumble" + ":" + ((Seq) x instanceof Jumble) + ":"); System.out.println("x.min()" + ":" + (x.min()) + ":"); } // okay, if you're still not convinced ... Seq b[] = new Seq[8]; b[0] = new Constant(8, 888); b[1] = new Jumble(new int[] {77, 78, 79}); b[2] = new Jumble(new int[] {81, 82}); b[3] = new Delta(4, 5, 1); b[4] = new Jumble(new int[] {}); b[5] = new Jumble(new int[] {10, 1, 2, 3, 4, 5, 10, -1, -2, -3, -4}); b[6] = new Delta(11, 0, 5); b[7] = new Constant(22, 222); for (int k = 0; k < 16; k++) { System.out.print("b[k%8]" + ":"); System.out.print(b[k % 8]); System.out.println(":"); System.out.println( "(Seq)b[k%8] instanceof Constant" + ":" + ((Seq) b[k % 8] instanceof Constant) + ":"); System.out.println( "(Seq)b[k%8] instanceof Delta" + ":" + ((Seq) b[k % 8] instanceof Delta) + ":"); System.out.println( "(Seq)b[k%8] instanceof Jumble" + ":" + ((Seq) b[k % 8] instanceof Jumble) + ":"); System.out.println("b[k%8].min()" + ":" + (b[k % 8].min()) + ":"); } // did you copy Jumble's array? int[] aa = {8, 4, 11}; Jumble jj1 = new Jumble(aa); System.out.print("jj1" + ":"); System.out.print(jj1); System.out.println(":"); System.out.println( "(Seq)jj1 instanceof Constant" + ":" + ((Seq) jj1 instanceof Constant) + ":"); System.out.println("(Seq)jj1 instanceof Delta" + ":" + ((Seq) jj1 instanceof Delta) + ":"); System.out.println("(Seq)jj1 instanceof Jumble" + ":" + ((Seq) jj1 instanceof Jumble) + ":"); System.out.println("jj1.min()" + ":" + (jj1.min()) + ":"); aa[1] = 9999; Jumble jj2 = new Jumble(aa); System.out.print("jj2" + ":"); System.out.print(jj2); System.out.println(":"); System.out.println( "(Seq)jj2 instanceof Constant" + ":" + ((Seq) jj2 instanceof Constant) + ":"); System.out.println("(Seq)jj2 instanceof Delta" + ":" + ((Seq) jj2 instanceof Delta) + ":"); System.out.println("(Seq)jj2 instanceof Jumble" + ":" + ((Seq) jj2 instanceof Jumble) + ":"); System.out.println("jj2.min()" + ":" + (jj2.min()) + ":"); // jj1 shouldn't have been changed System.out.print("jj1" + ":"); System.out.print(jj1); System.out.println(":"); System.out.println( "(Seq)jj1 instanceof Constant" + ":" + ((Seq) jj1 instanceof Constant) + ":"); System.out.println("(Seq)jj1 instanceof Delta" + ":" + ((Seq) jj1 instanceof Delta) + ":"); System.out.println("(Seq)jj1 instanceof Jumble" + ":" + ((Seq) jj1 instanceof Jumble) + ":"); System.out.println("jj1.min()" + ":" + (jj1.min()) + ":"); System.exit(0); }
/** * Compute the difference between original and revised sequences. * * @param orig The original sequence. * @param rev The revised sequence to be compared with the original. * @return A Revision object describing the differences. * @throws DifferenciationFailedException if the diff could not be computed. */ public Revision diff(Object[] orig, Object[] rev) throws DifferentiationFailedException { // create map eqs, such that for each item in both orig and rev // eqs(item) = firstOccurrence(item, orig); Map eqs = buildEqSet(orig, rev); // create an array such that // indx[i] = NOT_FOUND_i if orig[i] is not in rev // indx[i] = firstOccurrence(orig[i], orig) int[] indx = buildIndex(eqs, orig, NOT_FOUND_i); // create an array such that // jndx[j] = NOT_FOUND_j if orig[j] is not in rev // jndx[j] = firstOccurrence(rev[j], orig) int[] jndx = buildIndex(eqs, rev, NOT_FOUND_j); // what in effect has been done is to build a unique hash // for each item that is in both orig and rev // and to label each item in orig and new with that hash value // or a marker that the item is not common to both. eqs = null; // let gc know we're done with this Revision deltas = new Revision(); // !!! new Revision() int i = 0; int j = 0; // skip matching // skip leading items that are equal // could be written // for (i=0; indx[i] != EOS && indx[i] == jndx[i]; i++); // j = i; for (; indx[i] != EOS && indx[i] == jndx[j]; i++, j++) { /* void */ } while (indx[i] != jndx[j]) { // only equal if both == EOS // they are different int ia = i; int ja = j; // size of this delta do { // look down rev for a match // stop at a match // or if the FO(rev[j]) > FO(orig[i]) // or at the end while (jndx[j] < 0 || jndx[j] < indx[i]) { j++; } // look down orig for a match // stop at a match // or if the FO(orig[i]) > FO(rev[j]) // or at the end while (indx[i] < 0 || indx[i] < jndx[j]) { i++; } // this doesn't do a compare each line with each other line // so it won't find all matching lines } while (indx[i] != jndx[j]); // on exit we have a match // they are equal, reverse any exedent matches // it is possible to overshoot, so count back matching items while (i > ia && j > ja && indx[i - 1] == jndx[j - 1]) { --i; --j; } deltas.addDelta(Delta.newDelta(new Chunk(orig, ia, i - ia), new Chunk(rev, ja, j - ja))); // skip matching for (; indx[i] != EOS && indx[i] == jndx[j]; i++, j++) { /* void */ } } return deltas; }