JSync

Java library to synchronize two sets

License

License

GroupId

GroupId

com.crivano
ArtifactId

ArtifactId

jsync
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

JSync
Java library to synchronize two sets
Project URL

Project URL

https://github.com/crivano/jsync
Source Code Management

Source Code Management

https://github.com/crivano/jsync.git

Download jsync

How to add to project

<!-- https://jarcasting.com/artifacts/com.crivano/jsync/ -->
<dependency>
    <groupId>com.crivano</groupId>
    <artifactId>jsync</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.crivano/jsync/
implementation 'com.crivano:jsync:1.0.0'
// https://jarcasting.com/artifacts/com.crivano/jsync/
implementation ("com.crivano:jsync:1.0.0")
'com.crivano:jsync:jar:1.0.0'
<dependency org="com.crivano" name="jsync" rev="1.0.0">
  <artifact name="jsync" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.crivano', module='jsync', version='1.0.0')
)
libraryDependencies += "com.crivano" % "jsync" % "1.0.0"
[com.crivano/jsync "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.30

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

JSync

JSync is a simple open-source library that analyses two sets of POJOs, that may be linked to form graphs, and returns the operations that are necessary to go from the former graph to the latter.

It may seem to be an easy task, but it's not so. Detection of changes require a nesting batch algorithm involving all objects on both graphs. And, the resulting list of operations should be ordered based on the level of dependency of each object.

There are many applications for this kind of synchronization, but the most common is to record changes in a database. For that, the current objects on the database can be provided as the "old" graph and the "new" graph can be filled with the desired outcome. JSync will create a list of operations that may be added to the database to update the graph. In this situation, operations will be executed only if differences are detected.

Example / Usage

Melhods addOld and addNew should be used to populate both graphs with objects. Then, sync method can be called to produce a list of operations.

Some annotations may be added to POJOs in order to ignore fields both for the similarity check and for the computing of dependency levels.

A complete example can be found among the unit tests:

public class Foo implements Synchronizable {
	@IgnoreForSimilarity
	Long id;
	@IgnoreForDependencyLevel
	List<Bar> bars;
	boolean a;
	boolean b;
}

public class Bar implements Synchronizable {
	@IgnoreForSimilarity
	Long id;
	Foo foo;
	boolean changed;
}

@Test
public void test23FooChanged() {
	Foo oldFoo1 = new Foo(1L, null, false);
	Bar oldBar1 = new Bar(1L, oldFoo1, false);
	Foo newFoo1 = new Foo(1L, null, true);
	Bar newBar1 = new Bar(1L, newFoo1, false);
	Bar newBar2 = new Bar(2L, newFoo1, false);
	
	Synchronizer sync = new Synchronizer();
	sync.addOld(oldFoo1);
	sync.addOld(oldBar1);
	sync.addNew(newFoo1);
	sync.addNew(newBar1);
	sync.addNew(newBar2);
	
	List<Operation> l = sync.sync();
	
	assertEquals(3, l.size());
	assertEquals(Operator.UPDATE, l.get(0).getOperator());
	assertEquals(oldFoo1, l.get(0).getOld());
	assertEquals(newFoo1, l.get(0).getNew());
	assertEquals(0, l.get(0).getDependencyLevel());
	assertEquals(Operator.INSERT, l.get(1).getOperator());
	assertEquals(newBar2, l.get(1).getNew());
	assertEquals(newBar2.foo, newFoo1);
	assertEquals(1, l.get(1).getDependencyLevel());
	assertEquals(Operator.UPDATE, l.get(2).getOperator());
	assertEquals(oldBar1, l.get(2).getOld());
	assertEquals(newBar1, l.get(2).getNew());
	assertEquals(newBar1.foo, newFoo1);
	assertEquals(1, l.get(2).getDependencyLevel());
}

Versions

Version
1.0.0