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

155 lines
4.1 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.Arrays;
import java.util.Collections;
2013-08-23 16:59:50 +02:00
import java.util.List;
import com.pahimar.ee3.lib.Strings;
/**
* 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 value;
public final float recoveryPercent;
private final List<EmcComponent> components;
2013-08-23 16:59:50 +02:00
public EmcValue() {
this(0F, 1F, new ArrayList<EmcComponent>());
2013-08-23 16:59:50 +02:00
}
public EmcValue(float value) {
this(value, 1F, new ArrayList<EmcComponent>());
2013-08-23 16:59:50 +02:00
}
public EmcValue(float value, float recoveryPercent) {
2013-09-07 04:44:31 +02:00
this(value, recoveryPercent, new ArrayList<EmcComponent>());
2013-08-23 16:59:50 +02:00
}
public EmcValue(float value, List<EmcComponent> components) {
2013-08-23 16:59:50 +02:00
2013-09-07 04:44:31 +02:00
this(value, 1F, collateComponents(components));
2013-08-23 16:59:50 +02:00
}
public EmcValue(float value, float recoveryPercent, List<EmcComponent> components) {
2013-08-23 16:59:50 +02:00
this.value = value;
this.recoveryPercent = recoveryPercent;
2013-09-07 04:44:31 +02:00
this.components = collateComponents(components);
2013-08-23 16:59:50 +02:00
}
public List<EmcComponent> getComponents() {
2013-09-07 04:44:31 +02:00
return components;
2013-08-23 16:59:50 +02:00
}
public EmcComponent getComponentByType(EmcType type) {
2013-08-23 16:59:50 +02:00
EmcComponent[] componentArray = (EmcComponent[]) components.toArray();
Arrays.sort(componentArray);
return componentArray[type.ordinal()];
2013-08-23 16:59:50 +02:00
}
@Override
public boolean equals(Object object) {
if (!(object instanceof EmcValue)) {
return false;
}
EmcValue emcValue = (EmcValue) object;
return ((value == emcValue.value) && (recoveryPercent == emcValue.recoveryPercent) && (components.equals(emcValue.getComponents())));
2013-08-23 16:59:50 +02:00
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("V:%s%sRP:%s%s[", value, Strings.TOKEN_DELIMITER, recoveryPercent, Strings.TOKEN_DELIMITER));
List<EmcComponent> componentArray = this.components;
Collections.sort(componentArray);
2013-08-23 16:59:50 +02:00
2013-09-07 04:44:31 +02:00
int i = 0;
for (EmcComponent component : componentArray) {
2013-09-07 04:44:31 +02:00
stringBuilder.append(String.format("%s:%s", component.getType(), component.getRatioWeight()));
i++;
if (i < componentArray.size()) {
stringBuilder.append(String.format("%s", Strings.TOKEN_DELIMITER));
}
2013-08-23 16:59:50 +02:00
}
stringBuilder.append("]");
return stringBuilder.toString();
}
@Override
public int hashCode() {
int hashCode = 1;
hashCode = 37 * hashCode + Float.floatToIntBits(value);
hashCode = 37 * hashCode + Float.floatToIntBits(recoveryPercent);
hashCode = 37 * hashCode + components.hashCode();
2013-08-23 16:59:50 +02:00
return hashCode;
}
@Override
2013-09-07 04:44:31 +02:00
public int compareTo(EmcValue emcValue) {
if (Float.compare(this.value, emcValue.value) == 0) {
if (Float.compare(this.recoveryPercent, emcValue.recoveryPercent) == 0) {
return (this.components.hashCode() - emcValue.components.hashCode());
}
else {
return Float.compare(this.recoveryPercent, emcValue.recoveryPercent);
}
}
else {
return Float.compare(this.value, emcValue.value);
}
}
private static List<EmcComponent> collateComponents(List<EmcComponent> uncollatedComponents) {
Integer[] componentCount = new Integer[7];
for (EmcComponent emcComponent : uncollatedComponents) {
if (componentCount[emcComponent.getType().ordinal()] == null) {
componentCount[emcComponent.getType().ordinal()] = new Integer(0);
}
componentCount[emcComponent.getType().ordinal()] = new Integer(componentCount[emcComponent.getType().ordinal()].intValue() + emcComponent.getRatioWeight());
}
List<EmcComponent> collatedComponents = new ArrayList<EmcComponent>();
for (int i = 0; i < 7; i++) {
if (componentCount[i] != null) {
collatedComponents.add(new EmcComponent(EmcType.values()[i], componentCount[i].intValue()));
}
}
return collatedComponents;
}
2013-08-23 16:59:50 +02:00
}