2013-08-23 16:59:50 +02:00
|
|
|
package com.pahimar.ee3.emc;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2013-09-06 22:19:11 +02:00
|
|
|
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)
|
|
|
|
*
|
|
|
|
*/
|
2013-09-06 04:21:55 +02:00
|
|
|
public class EmcValue implements Comparable<EmcValue> {
|
2013-08-23 16:59:50 +02:00
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
public final float value;
|
2013-09-06 22:19:11 +02:00
|
|
|
public final float recoveryPercent;
|
|
|
|
private final List<EmcComponent> components;
|
2013-10-06 04:46:29 +02:00
|
|
|
public final int sumComponentRatios;
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
public EmcValue() {
|
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
this(0F, 1F, new ArrayList<EmcComponent>());
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
2013-10-18 02:40:45 +02:00
|
|
|
public EmcValue(int value) {
|
|
|
|
|
|
|
|
this(Integer.valueOf(value).floatValue(), 1F, new ArrayList<EmcComponent>());
|
|
|
|
}
|
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
public EmcValue(float value) {
|
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
this(value, 1F, new ArrayList<EmcComponent>());
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
public EmcValue(float value, float recoveryPercent) {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
this(value, recoveryPercent, new ArrayList<EmcComponent>());
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-09-06 22:19:11 +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
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-09-10 03:48:34 +02:00
|
|
|
public EmcValue(float value, EmcComponent component) {
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-09-10 03:48:34 +02:00
|
|
|
this(value, 1F, Arrays.asList(component));
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
public EmcValue(float value, float recoveryPercent, List<EmcComponent> components) {
|
2013-08-23 16:59:50 +02:00
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
this.value = value;
|
|
|
|
this.recoveryPercent = recoveryPercent;
|
2013-09-07 04:44:31 +02:00
|
|
|
this.components = collateComponents(components);
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-10-06 04:46:29 +02:00
|
|
|
int count = 0;
|
|
|
|
for (EmcComponent component : components) {
|
|
|
|
count += component.getRatioWeight();
|
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-10-06 04:46:29 +02:00
|
|
|
this.sumComponentRatios = count;
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<EmcComponent> getComponents() {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
|
|
|
return components;
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
public EmcComponent getComponentByType(EmcType type) {
|
2013-08-23 16:59:50 +02:00
|
|
|
|
2013-10-06 04:46:29 +02:00
|
|
|
for (EmcComponent component : components) {
|
|
|
|
if (component.getType().equals(type)) {
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-10-06 04:46:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-10-06 04:46:29 +02:00
|
|
|
public float getComponentValueByType(EmcType type) {
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-10-06 04:46:29 +02:00
|
|
|
EmcComponent emcComponent = getComponentByType(type);
|
|
|
|
if (emcComponent != null) {
|
|
|
|
if (sumComponentRatios > 0) {
|
2013-10-18 02:40:45 +02:00
|
|
|
return value * (emcComponent.getRatioWeight() / (float) this.sumComponentRatios);
|
2013-10-06 04:46:29 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-10-06 04:46:29 +02:00
|
|
|
return 0f;
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
2013-10-18 04:38:15 +02:00
|
|
|
|
|
|
|
public float[] getComponentSubValues() {
|
|
|
|
|
|
|
|
float[] componentSubValues = new float[EmcType.TYPES.length];
|
|
|
|
|
|
|
|
for (int i = 0; i < componentSubValues.length; i++) {
|
|
|
|
componentSubValues[i] = getComponentValueByType(EmcType.TYPES[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return componentSubValues;
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object object) {
|
|
|
|
|
|
|
|
if (!(object instanceof EmcValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EmcValue emcValue = (EmcValue) object;
|
|
|
|
|
2013-10-18 02:40:45 +02:00
|
|
|
return (Float.compare(this.value, emcValue.value) == 0) && (Float.compare(this.recoveryPercent, emcValue.recoveryPercent) == 0) && components.equals(emcValue.getComponents());
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
stringBuilder.append(String.format("V:%s%sRP:%s%s[", value, Strings.TOKEN_DELIMITER, recoveryPercent, Strings.TOKEN_DELIMITER));
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
List<EmcComponent> componentArray = this.components;
|
|
|
|
Collections.sort(componentArray);
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-09-07 04:44:31 +02:00
|
|
|
int i = 0;
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-09-06 22:19:11 +02:00
|
|
|
for (EmcComponent component : componentArray) {
|
2013-09-07 04:47:12 +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-09-07 04:44:31 +02:00
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
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);
|
2013-09-06 22:19:11 +02:00
|
|
|
hashCode = 37 * hashCode + Float.floatToIntBits(recoveryPercent);
|
|
|
|
hashCode = 37 * hashCode + components.hashCode();
|
2013-08-23 16:59:50 +02:00
|
|
|
|
|
|
|
return hashCode;
|
|
|
|
}
|
2013-09-06 04:21:55 +02:00
|
|
|
|
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.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);
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-09-10 19:29:30 +02:00
|
|
|
return Float.compare(this.value, emcValue.value);
|
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[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>();
|
2013-10-18 02:40:45 +02:00
|
|
|
|
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-10-18 02:40:45 +02:00
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
return collatedComponents;
|
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|