Add docs for IEnergy.

This commit is contained in:
raoulvdberge
2018-07-03 13:03:29 +02:00
parent d56973b336
commit 23a9b33a25
2 changed files with 51 additions and 21 deletions

View File

@@ -4,18 +4,48 @@ import com.raoulvdberge.refinedstorage.api.util.Action;
import java.util.UUID;
/**
* An energy container.
*/
public interface IEnergy {
void decreaseCapacity(UUID id, int amount);
/**
* @param id id of the storage
* @param amount the amount
*/
void removeCapacity(UUID id, int amount);
int extract(int amount, Action action);
/**
* @param id id of the storage
* @param amount the amount
*/
void addCapacity(UUID id, int amount);
/**
* @return the capacity
*/
int getCapacity();
/**
* @return the amount stored
*/
int getStored();
void increaseCapacity(UUID id, int amount);
/**
* @param amount the amount stored
*/
void setStored(int amount);
/**
* @param amount the amount
* @param action the action
* @return the energy extracted
*/
int extract(int amount, Action action);
/**
* @param amount the amount
* @param action the action
* @return the energy inserted
*/
int insert(int amount, Action action);
void setStored(int energyAmount);
}

View File

@@ -13,28 +13,28 @@ public final class Energy implements IEnergy {
protected int capacity;
protected int energy;
private final Map<UUID, Integer> energyStorages;
private final Map<UUID, Integer> storages;
public Energy(int controllerCapacity) {
this.energyStorages = new Object2ObjectOpenHashMap<>();
this.energyStorages.put(DEFAULT_UUID, controllerCapacity);
this.storages = new Object2ObjectOpenHashMap<>();
this.storages.put(DEFAULT_UUID, controllerCapacity);
calculateCapacity();
}
private void calculateCapacity() {
long newCapacity = energyStorages.values().stream().mapToLong(Long::valueOf).sum();
long newCapacity = storages.values().stream().mapToLong(Long::valueOf).sum();
this.capacity = (int) Math.min(newCapacity, Integer.MAX_VALUE);
}
@Override
public void decreaseCapacity(UUID id, int amount) {
public void removeCapacity(UUID id, int amount) {
if (id.equals(DEFAULT_UUID)) {
return;
}
this.energyStorages.remove(id);
this.storages.remove(id);
calculateCapacity();
}
@@ -45,13 +45,13 @@ public final class Energy implements IEnergy {
return 0;
}
int energyExtracted = Math.min(energy, maxExtract);
int extracted = Math.min(energy, maxExtract);
if (action == Action.PERFORM) {
energy -= energyExtracted;
energy -= extracted;
}
return energyExtracted;
return extracted;
}
@Override
@@ -65,12 +65,12 @@ public final class Energy implements IEnergy {
}
@Override
public void increaseCapacity(UUID id, int amount) {
public void addCapacity(UUID id, int amount) {
if (id.equals(DEFAULT_UUID) || amount <= 0) {
return;
}
this.energyStorages.merge(id, amount, (k, v) -> amount);
this.storages.merge(id, amount, (k, v) -> amount);
calculateCapacity();
}
@@ -81,17 +81,17 @@ public final class Energy implements IEnergy {
return 0;
}
int energyReceived = Math.min(capacity - energy, maxReceive);
int inserted = Math.min(capacity - energy, maxReceive);
if (action == Action.PERFORM) {
energy += energyReceived;
energy += inserted;
}
return energyReceived;
return inserted;
}
@Override
public void setStored(int energy) {
this.energy = Math.min(energy, this.capacity);
public void setStored(int amount) {
this.energy = Math.min(amount, this.capacity);
}
}