package de.retest.recheck.ui.diff.meta;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Spliterator;
import java.util.TreeSet;
import java.util.function.Consumer;

import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

@ToString
@EqualsAndHashCode
@RequiredArgsConstructor( access = AccessLevel.PRIVATE )
public final class MetadataDifference implements Iterable<MetadataElementDifference>, Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	private final Set<MetadataElementDifference> differences;

	// For JAXB
	private MetadataDifference() {
		this( Collections.emptySet() );
	}

	public static MetadataDifference empty() {
		return new MetadataDifference( Collections.emptySet() );
	}

	public static MetadataDifference of( final Set<MetadataElementDifference> differences ) {
		final Set<MetadataElementDifference> unique =
				new TreeSet<>( Comparator.comparing( MetadataElementDifference::getKey ) );
		unique.addAll( differences );
		return new MetadataDifference( new LinkedHashSet<>( unique ) );
	}

	public boolean isEmpty() {
		return differences.isEmpty();
	}

	public int size() {
		return differences.size();
	}

	@Override
	public Iterator<MetadataElementDifference> iterator() {
		return differences.iterator();
	}

	@Override
	public Spliterator<MetadataElementDifference> spliterator() {
		return differences.spliterator();
	}

	@Override
	public void forEach( final Consumer<? super MetadataElementDifference> action ) {
		differences.forEach( action );
	}
}

@@ -681,16 +681,92 @@
  = 1L;%0A%0A
+%09private static final MetadataDifference EMPTY = new MetadataDifference();%0A%0A
 %09@Getter
@@ -830,21 +830,8 @@
 s;%0A%0A
-%09// For JAXB%0A
 %09pri
@@ -953,56 +953,13 @@
 urn 
-new MetadataDifference( Collections.emptySet() )
+EMPTY
 ;%0A%09%7D
@@ -1051,16 +1051,72 @@
 ces ) %7B%0A
+%09%09if ( differences.isEmpty() ) %7B%0A%09%09%09return empty();%0A%09%09%7D%0A
 %09%09final 

Reuse empty instance