From 6de07293283c57e8d587c4131121c696920d1c38 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 30 Sep 2013 17:29:18 +0200 Subject: [PATCH] Immutable Couple Class pls --- src/com/massivecraft/mcore/Couple.java | 99 ++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/com/massivecraft/mcore/Couple.java diff --git a/src/com/massivecraft/mcore/Couple.java b/src/com/massivecraft/mcore/Couple.java new file mode 100644 index 00000000..99daefbc --- /dev/null +++ b/src/com/massivecraft/mcore/Couple.java @@ -0,0 +1,99 @@ +package com.massivecraft.mcore; + +import java.io.Serializable; +import java.util.Map.Entry; + +import com.massivecraft.mcore.util.MUtil; + +public class Couple implements Entry, Cloneable, Serializable +{ + // -------------------------------------------- // + // CONSTANTS + // -------------------------------------------- // + + private static final transient long serialVersionUID = 1L; + + // -------------------------------------------- // + // FIELDS: RAW + // -------------------------------------------- // + + private final A first; + public A getFirst() { return this.first; }; + + private final B second; + public B getSecond() { return this.second; }; + + // -------------------------------------------- // + // FIELDS: WITH + // -------------------------------------------- // + + public Couple withFirst(A first) { return valueOf(first, second); } + + public Couple withSecond(B second) { return valueOf(first, second); } + + // -------------------------------------------- // + // PRIVATE CONSTRUCTOR + // -------------------------------------------- // + + private Couple(A first, B second) + { + this.first = first; + this.second = second; + } + + // -------------------------------------------- // + // OVERRIDE: ENTRY + // -------------------------------------------- // + + @Override + public A getKey() + { + return this.first; + } + + @Override + public B getValue() + { + return this.second; + } + + @Override + public B setValue(B arg0) + { + throw new IllegalStateException("This entry is a couple which is immutable."); + } + + // -------------------------------------------- // + // FACTORY: VALUE OF + // -------------------------------------------- // + + public static Couple valueOf(A first, B second) + { + return new Couple(first, second); + } + + // -------------------------------------------- // + // EQUALS + // -------------------------------------------- // + + @Override + public boolean equals(Object derpObject) + { + if (derpObject == null) return false; + if (!(derpObject instanceof Couple)) return false; + Couple derp = (Couple)derpObject; + return MUtil.equals(this.getFirst(), derp.getFirst()) && MUtil.equals(this.getSecond(), derp.getSecond()); + } + + // -------------------------------------------- // + // CLONE + // -------------------------------------------- // + + @Override + public Couple clone() + { + return this; + } + + +}