equivalent-exchange-3/common/com/pahimar/ee3/emc/EmcValue.java

220 lines
5.9 KiB
Java
Raw Normal View History

2013-08-23 16:59:50 +02:00
package com.pahimar.ee3.emc;
import java.util.ArrayList;
import java.util.Collections;
2013-08-23 16:59:50 +02:00
import java.util.List;
/**
* Equivalent-Exchange-3
*
* EMCEntry
*
* @author pahimar
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*
*/
public class EmcValue implements Comparable<EmcValue> {
2013-08-23 16:59:50 +02:00
public final float recoveryPercent;
public final float[] components;
2013-08-23 16:59:50 +02:00
public EmcValue() {
this(1F, new float[EmcType.TYPES.length]);
2013-08-23 16:59:50 +02:00
}
public EmcValue(int value) {
this((float) value);
}
2013-08-23 16:59:50 +02:00
public EmcValue(float value) {
this(value, 1F);
2013-08-23 16:59:50 +02:00
}
public EmcValue(float value, EmcComponent component) {
2013-09-07 04:47:12 +02:00
this(value, 1F, component);
2013-08-23 16:59:50 +02:00
}
2013-09-07 04:47:12 +02:00
public EmcValue(float value, float recoveryPercent, EmcComponent component) {
2013-08-23 16:59:50 +02:00
this.recoveryPercent = recoveryPercent;
this.components = new float[EmcType.TYPES.length];
this.components[component.type.ordinal()] = value;
2013-08-23 16:59:50 +02:00
}
public EmcValue(int value, float recoveryPercent) {
this((float) value, recoveryPercent);
}
2013-08-23 16:59:50 +02:00
public EmcValue(float value, float recoveryPercent) {
2013-08-23 16:59:50 +02:00
this.recoveryPercent = recoveryPercent;
this.components = new float[EmcType.TYPES.length];
this.components[EmcType.DEFAULT.ordinal()] = value;
}
public EmcValue(float[] components) {
this(1F, components);
2013-08-23 16:59:50 +02:00
}
public EmcValue(float recoveryPercent, float[] components) {
2013-09-07 04:47:12 +02:00
this.recoveryPercent = recoveryPercent;
this.components = components;
2013-08-23 16:59:50 +02:00
}
2013-09-07 04:47:12 +02:00
public EmcValue(int value, List<EmcComponent> componentList) {
2013-08-23 16:59:50 +02:00
this((float) value, componentList);
}
public EmcValue(float value, List<EmcComponent> componentList) {
this(value, 1F, componentList);
2013-10-06 04:46:29 +02:00
}
public EmcValue(float value, float recoveryPercent, List<EmcComponent> componentList) {
this.recoveryPercent = recoveryPercent;
this.components = new float[EmcType.TYPES.length];
List<EmcComponent> collatedComponents = collateComponents(componentList);
int totalComponents = 0;
for (EmcComponent component : collatedComponents) {
if (component.weight > 0) {
totalComponents += component.weight;
2013-10-06 04:46:29 +02:00
}
}
if (totalComponents > 0) {
for (EmcComponent component : collatedComponents) {
if (component.weight > 0) {
this.components[component.type.ordinal()] = value * (component.weight * 1F / totalComponents);
}
}
}
else {
this.components[EmcType.DEFAULT.ordinal()] = value;
}
2013-08-23 16:59:50 +02:00
}
public float getValue() {
float sumSubValues = 0;
for (float subValue : this.components) {
if (subValue > 0) {
sumSubValues += subValue;
}
}
return sumSubValues;
}
2013-08-23 16:59:50 +02:00
@Override
public boolean equals(Object object) {
if (!(object instanceof EmcValue)) {
return false;
}
return (compareTo((EmcValue) object) == 0);
2013-08-23 16:59:50 +02:00
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
// TODO Intelligible output
stringBuilder.append(String.format("%s @ %s", components.toString(), recoveryPercent));
2013-08-23 16:59:50 +02:00
return stringBuilder.toString();
}
@Override
public int hashCode() {
int hashCode = 1;
hashCode = 37 * hashCode + Float.floatToIntBits(getValue());
hashCode = 37 * hashCode + Float.floatToIntBits(recoveryPercent);
for (float subValue : components) {
hashCode = 37 * hashCode + Float.floatToIntBits(subValue);
}
2013-08-23 16:59:50 +02:00
return hashCode;
}
2013-09-07 04:47:12 +02:00
@Override
public int compareTo(EmcValue emcValue) {
2013-09-10 19:29:30 +02:00
if (emcValue instanceof EmcValue) {
if (Float.compare(this.getValue(), emcValue.getValue()) == 0) {
2013-09-10 19:29:30 +02:00
if (Float.compare(this.recoveryPercent, emcValue.recoveryPercent) == 0) {
return compareComponents(this.components, emcValue.components);
2013-09-10 19:29:30 +02:00
}
else {
return Float.compare(this.recoveryPercent, emcValue.recoveryPercent);
}
2013-09-07 04:47:12 +02:00
}
else {
return Float.compare(this.getValue(), emcValue.getValue());
2013-09-07 04:47:12 +02:00
}
}
else {
2013-09-10 19:29:30 +02:00
return 1;
2013-09-07 04:47:12 +02:00
}
}
private static List<EmcComponent> collateComponents(List<EmcComponent> uncollatedComponents) {
Integer[] componentCount = new Integer[EmcType.TYPES.length];
2013-09-07 04:47:12 +02:00
for (EmcComponent emcComponent : uncollatedComponents) {
if (componentCount[emcComponent.type.ordinal()] == null) {
componentCount[emcComponent.type.ordinal()] = new Integer(0);
2013-09-07 04:47:12 +02:00
}
if (emcComponent.weight >= 0) {
componentCount[emcComponent.type.ordinal()] = new Integer(componentCount[emcComponent.type.ordinal()].intValue() + emcComponent.weight);
}
2013-09-07 04:47:12 +02:00
}
List<EmcComponent> collatedComponents = new ArrayList<EmcComponent>();
2013-10-06 04:46:29 +02:00
for (int i = 0; i < EmcType.TYPES.length; i++) {
2013-09-07 04:47:12 +02:00
if (componentCount[i] != null) {
2013-10-06 04:46:29 +02:00
collatedComponents.add(new EmcComponent(EmcType.TYPES[i], componentCount[i].intValue()));
2013-09-07 04:47:12 +02:00
}
}
2013-10-06 04:46:29 +02:00
Collections.sort(collatedComponents);
2013-09-07 04:47:12 +02:00
return collatedComponents;
}
private static int compareComponents(float[] first, float[] second) {
if (first.length == EmcType.TYPES.length && second.length == EmcType.TYPES.length) {
for (EmcType emcType : EmcType.TYPES) {
if (Float.compare(first[emcType.ordinal()], second[emcType.ordinal()]) != 0) {
return Float.compare(first[emcType.ordinal()], second[emcType.ordinal()]);
}
}
return 0;
}
else {
throw new ArrayIndexOutOfBoundsException();
}
}
2013-08-23 16:59:50 +02:00
}