Last active
October 9, 2021 05:31
-
-
Save onacit/4cb92ba58cd8e8ab40d7e734e1f86ce1 to your computer and use it in GitHub Desktop.
Myers–Briggs Type Indicator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package p_4cb92ba58cd8e8ab40d7e734e1f86ce1; | |
import javax.validation.constraints.NotNull; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Objects; | |
import static java.util.Objects.requireNonNull; | |
public final class MyersBriggsTypeIndicator { | |
private interface Dichotomy<E extends Enum<E> & Dichotomy<E>> { | |
} | |
/** | |
* Constants for attitudes. | |
*/ | |
public enum Attitude implements Dichotomy<Attitude> { | |
/** | |
* Constant for <u>Extraversion</u>. | |
*/ | |
E, | |
/** | |
* Constant for <u>Introversion</u>. | |
*/ | |
I; | |
} | |
/** | |
* An interface for psychological functions. | |
* | |
* @param <E> enum type parameter. | |
*/ | |
private interface Function<E extends Enum<E> & Function<E>> extends Dichotomy<E> { | |
} | |
/** | |
* Constants for perceiving functions. | |
*/ | |
public enum PerceivingFunction implements Function<PerceivingFunction> { | |
/** | |
* Constant for <u>Sensing</u>. | |
*/ | |
S, | |
/** | |
* Constant for <u>Intuition</u>. | |
*/ | |
N; | |
} | |
/** | |
* Constants for judging functions. | |
*/ | |
public enum JudgingFunction implements Function<JudgingFunction> { | |
/** | |
* Constant for <u>Thinking</u>. | |
*/ | |
T, | |
/** | |
* Constant for <u>Feeling</u>. | |
*/ | |
F; | |
} | |
/** | |
* Constants for lifestyle preferences. | |
*/ | |
public enum LifestylePreference implements Dichotomy<LifestylePreference> { | |
/** | |
* Constant for <u>Judging</u>. | |
*/ | |
J, | |
/** | |
* Constant for <u>Perception</u>. | |
*/ | |
P; | |
} | |
private static final Map<String, MyersBriggsTypeIndicator> VALUES; | |
static { | |
final Map<String, MyersBriggsTypeIndicator> map = new HashMap<>(); | |
for (final Attitude a : Attitude.values()) { | |
for (final PerceivingFunction p : PerceivingFunction.values()) { | |
for (final JudgingFunction j : JudgingFunction.values()) { | |
for (final LifestylePreference l : LifestylePreference.values()) { | |
final MyersBriggsTypeIndicator instance = new MyersBriggsTypeIndicator(a, p, j, l); | |
map.put(instance.toString(), instance); | |
} | |
} | |
} | |
} | |
VALUES = Collections.unmodifiableMap(map); | |
} | |
private static String result(final Attitude attitude, final PerceivingFunction perceivingFunction, | |
final JudgingFunction judgingFunction, final LifestylePreference lifestylePreference) { | |
requireNonNull(attitude, "attitude is null"); | |
requireNonNull(perceivingFunction, "perceivingFunction is null"); | |
requireNonNull(judgingFunction, "judgingFunction is null"); | |
requireNonNull(lifestylePreference, "lifestylePreference is null"); | |
return attitude.name() + perceivingFunction.name() + judgingFunction.name() + lifestylePreference.name(); | |
} | |
/** | |
* Returns the value of specified dichotomies. | |
* | |
* @param attitude a value of {@link Attitude}. | |
* @param perceivingFunction a value of {@link PerceivingFunction}. | |
* @param judgingFunction a value of {@link JudgingFunction}. | |
* @param lifestylePreference a value of {@link LifestylePreference} | |
* @return the value of specified dichotomies. | |
*/ | |
public static MyersBriggsTypeIndicator valueOf(final Attitude attitude, final PerceivingFunction perceivingFunction, | |
final JudgingFunction judgingFunction, | |
final LifestylePreference lifestylePreference) { | |
final String result = result(attitude, perceivingFunction, judgingFunction, lifestylePreference); | |
return VALUES.get(result); | |
} | |
public static MyersBriggsTypeIndicator valueOf(final String result) { | |
requireNonNull(result, "result is null"); | |
return VALUES.get(result); | |
} | |
// ----------------------------------------------------------------------------------------------------------------- | |
private MyersBriggsTypeIndicator(final Attitude attitude, final PerceivingFunction perceivingFunction, | |
final JudgingFunction judgingFunction, | |
final LifestylePreference lifestylePreference) { | |
super(); | |
this.attitude = requireNonNull(attitude, "attitude is null"); | |
this.perceivingFunction = requireNonNull(perceivingFunction, "perceivingFunction is null"); | |
this.judgingFunction = requireNonNull(judgingFunction, "judgingFunction is null"); | |
this.lifestylePreference = requireNonNull(lifestylePreference, "lifestylePreference is null"); | |
} | |
// ----------------------------------------------------------------------------------------------------------------- | |
/** | |
* Returns a string representation of this value. | |
* | |
* @return a string representation of this value. | |
*/ | |
@Override | |
public String toString() { | |
return result(attitude, perceivingFunction, judgingFunction, lifestylePreference); | |
} | |
// ----------------------------------------------------------------------------------------------------------------- | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
MyersBriggsTypeIndicator that = (MyersBriggsTypeIndicator) o; | |
return attitude == that.attitude && perceivingFunction == that.perceivingFunction | |
&& judgingFunction == that.judgingFunction && lifestylePreference == that.lifestylePreference; | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(attitude, perceivingFunction, judgingFunction, lifestylePreference); | |
} | |
// ----------------------------------------------------------------------------------------------------------------- | |
public final @NotNull Attitude attitude; | |
public final @NotNull PerceivingFunction perceivingFunction; | |
public final @NotNull JudgingFunction judgingFunction; | |
public final @NotNull LifestylePreference lifestylePreference; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment