/** * Adds up two capacities, i.e. sums up each and every capacity dimension, and returns the * resulting Capacity. * * <p>Note that this assumes that capacity dimension can be added up. * * @param cap1 capacity to be added up * @param cap2 capacity to be added up * @return new capacity * @throws NullPointerException if one of the args is null */ public static Capacity addup(Capacity cap1, Capacity cap2) { if (cap1 == null || cap2 == null) throw new NullPointerException("arguments must not be null"); Capacity.Builder capacityBuilder = Capacity.Builder.newInstance(); for (int i = 0; i < Math.max(cap1.getNuOfDimensions(), cap2.getNuOfDimensions()); i++) { capacityBuilder.addDimension(i, cap1.get(i) + cap2.get(i)); } return capacityBuilder.build(); }
public static Capacity min(Capacity cap1, Capacity cap2) { if (cap1 == null || cap2 == null) throw new IllegalArgumentException("arg must not be null"); Capacity.Builder toReturnBuilder = Capacity.Builder.newInstance(); for (int i = 0; i < Math.max(cap1.getNuOfDimensions(), cap2.getNuOfDimensions()); i++) { toReturnBuilder.addDimension(i, Math.min(cap1.get(i), cap2.get(i))); } return toReturnBuilder.build(); }
/** * Subtracts cap2subtract from cap and returns the resulting Capacity. * * @param cap capacity to be subtracted from * @param cap2subtract capacity to subtract * @return new capacity * @throws NullPointerException if one of the args is null * @throws IllegalStateException if number of capacityDimensions of cap1 and cap2 are different * (i.e. <code>cap1.getNuOfDimension() != cap2.getNuOfDimension()</code>). */ public static Capacity subtract(Capacity cap, Capacity cap2subtract) { if (cap == null || cap2subtract == null) throw new NullPointerException("arguments must not be null"); Capacity.Builder capacityBuilder = Capacity.Builder.newInstance(); for (int i = 0; i < Math.max(cap.getNuOfDimensions(), cap2subtract.getNuOfDimensions()); i++) { int dimValue = cap.get(i) - cap2subtract.get(i); capacityBuilder.addDimension(i, dimValue); } return capacityBuilder.build(); }
/** * Returns true if this capacity is greater or equal than the capacity toCompare * * @param toCompare the capacity to compare * @return true if this capacity is greater or equal than toCompare * @throws NullPointerException if one of the args is null */ public boolean isGreaterOrEqual(Capacity toCompare) { if (toCompare == null) throw new NullPointerException(); for (int i = 0; i < Math.max(this.getNuOfDimensions(), toCompare.getNuOfDimensions()); i++) { if (this.get(i) < toCompare.get(i)) return false; } return true; }
/** * Divides every dimension of numerator capacity by the corresponding dimension of denominator * capacity, , and averages each quotient. * * <p>If both nominator.get(i) and denominator.get(i) equal to 0, dimension i is ignored. * * <p>If both capacities are have only dimensions with dimensionVal=0, it returns 0.0 * * @param numerator the numerator * @param denominator the denominator * @return quotient * @throws IllegalStateException if numerator.get(i) != 0 and denominator.get(i) == 0 */ public static double divide(Capacity numerator, Capacity denominator) { int nuOfDimensions = 0; double sumQuotients = 0.0; for (int index = 0; index < Math.max(numerator.getNuOfDimensions(), denominator.getNuOfDimensions()); index++) { if (numerator.get(index) != 0 && denominator.get(index) == 0) { throw new IllegalStateException("numerator > 0 and denominator = 0. cannot divide by 0"); } else if (numerator.get(index) == 0 && denominator.get(index) == 0) { continue; } else { nuOfDimensions++; sumQuotients += (double) numerator.get(index) / (double) denominator.get(index); } } if (nuOfDimensions > 0) return sumQuotients / (double) nuOfDimensions; return 0.0; }
/** * Returns the inverted capacity, i.e. it multiplies all capacity dimensions with -1. * * @param cap2invert capacity to be inverted * @return inverted capacity * @throws NullPointerException if one of the args is null */ public static Capacity invert(Capacity cap2invert) { if (cap2invert == null) throw new NullPointerException("arguments must not be null"); Capacity.Builder capacityBuilder = Capacity.Builder.newInstance(); for (int i = 0; i < cap2invert.getNuOfDimensions(); i++) { int dimValue = cap2invert.get(i) * -1; capacityBuilder.addDimension(i, dimValue); } return capacityBuilder.build(); }
/** * copy constructor * * @param capacity capacity to be copied */ Capacity(Capacity capacity) { this.dimensions = new int[capacity.getNuOfDimensions()]; for (int i = 0; i < capacity.getNuOfDimensions(); i++) { this.dimensions[i] = capacity.get(i); } }