package ch.ifocusit.livingdoc.plugin.mapping;

import com.google.common.collect.Ordering;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

public class MappingDefinition implements Comparable<MappingDefinition> {
    private Integer id;
    private String name;
    private String description;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Check the name
     */
    public MappingDefinition checkName() {
        name = name.trim().replace(" ", "_");
        return this;
    }

    @Override
    public int compareTo(MappingDefinition o) {
        return ORDERING.compare(this, o);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    private static final Ordering<MappingDefinition> ORDERING = Ordering
            // sort by id
            .natural().nullsFirst().onResultOf(MappingDefinition::getId)
            // and after by name
            .compound(Ordering.natural().nullsFirst().onResultOf(MappingDefinition::getName))
            // and after by description
            .compound(Ordering.natural().nullsFirst().onResultOf(MappingDefinition::getDescription))
            .nullsFirst();
}
@@ -1006,24 +1006,77 @@
  return 
-ORDERING
+BY_ID.compound(BY_NAME).compound(BY_DESCRIPTION).nullsFirst()
 .compare
@@ -1375,16 +1375,13 @@
 on%3E 
-ORDERING
+BY_ID
  = O
@@ -1391,163 +1391,120 @@
 ring
-%0A            // sort by id%0A            .natural().nullsFirst().onResultOf(MappingDefinition::getId)%0A            // and after by name%0A            .compound(
+.natural().onResultOf(MappingDefinition::getId);%0A    private static final Ordering%3CMappingDefinition%3E BY_NAME = 
 Orde
@@ -1573,72 +1573,80 @@
 ame)
-)
+;
 %0A    
-        // and after by description%0A            .compound(
+private static final Ordering%3CMappingDefinition%3E BY_DESCRIPTION = 
 Orde
@@ -1722,34 +1722,7 @@
 ion)
-)%0A            .nullsFirst()
 ;%0A%7D

correct casting