Skip to content

Commit

Permalink
Prepare Arithmetic for vector math (long term, not for next version)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed May 1, 2017
1 parent 1e5332a commit ffd14ae
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/main/java/ch/njol/skript/classes/Arithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package ch.njol.skript.classes;

/**
* Represents arithmetic for certain two types. Multiplication, division and
* power of methods are optional and may throw UnsupportedOperationExceptions.
* @param <A> the type of the absolute value
* @param <R> the type of the relative value
* @author Peter Güttinger
*/
public interface Arithmetic<A, R> {

Expand All @@ -32,4 +33,10 @@ public interface Arithmetic<A, R> {

public A subtract(A value, R difference);

public A multiply(A value, R multiplier);

public A divide(A value, R divider);

public A power(A value, R exponent);

}
40 changes: 36 additions & 4 deletions src/main/java/ch/njol/skript/classes/NumberArithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,52 @@
*/
public class NumberArithmetic implements Arithmetic<Number, Number> {

@SuppressWarnings("null")
@Override
public Number difference(final Number first, final Number second) {
return Math.abs(first.doubleValue() - second.doubleValue());
double result = Math.abs(first.doubleValue() - second.doubleValue());
if (result == (long) result)
return (long) result;
return result;
}

@Override
public Number add(final Number value, final Number difference) {
return new Double(value.doubleValue() + difference.doubleValue());
double result = value.doubleValue() + difference.doubleValue();
if (result == (long) result)
return (long) result;
return result;
}

@Override
public Number subtract(final Number value, final Number difference) {
return new Double(value.doubleValue() - difference.doubleValue());
double result = value.doubleValue() - difference.doubleValue();
if (result == (long) result)
return (long) result;
return result;
}

@Override
public Number multiply(Number value, Number multiplier) {
double result = value.doubleValue() * multiplier.doubleValue();
if (result == (long) result)
return (long) result;
return result;
}

@Override
public Number divide(Number value, Number divider) {
double result = value.doubleValue() / divider.doubleValue();
if (result == (long) result)
return (long) result;
return result;
}

@Override
public Number power(Number value, Number exponent) {
double result = Math.pow(value.doubleValue(), exponent.doubleValue());
if (result == (long) result)
return (long) result;
return result;
}

}
30 changes: 30 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,21 @@ public Timespan add(final Timespan value, final Timespan difference) {
public Timespan subtract(final Timespan value, final Timespan difference) {
return new Timespan(Math.max(0, value.getMilliSeconds() - difference.getMilliSeconds()));
}

@Override
public Timespan multiply(Timespan value, Timespan multiplier) {
throw new UnsupportedOperationException();
}

@Override
public Timespan divide(Timespan value, Timespan divider) {
throw new UnsupportedOperationException();
}

@Override
public Timespan power(Timespan value, Timespan exponent) {
throw new UnsupportedOperationException();
}
}));

// TODO remove
Expand Down Expand Up @@ -552,6 +567,21 @@ public Date add(final Date value, final Timespan difference) {
public Date subtract(final Date value, final Timespan difference) {
return new Date(value.getTimestamp() - difference.getMilliSeconds());
}

@Override
public Date multiply(Date value, Timespan multiplier) {
throw new UnsupportedOperationException();
}

@Override
public Date divide(Date value, Timespan divider) {
throw new UnsupportedOperationException();
}

@Override
public Date power(Date value, Timespan exponent) {
throw new UnsupportedOperationException();
}
}));

Classes.registerClass(new ClassInfo<>(Direction.class, "direction")
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/ch/njol/skript/hooks/economy/classes/Money.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public Money add(final Money value, final Money difference) {
public Money subtract(final Money value, final Money difference) {
return new Money(value.amount - difference.amount);
}

@Override
public Money multiply(Money value, Money multiplier) {
return new Money(value.getAmount() * multiplier.getAmount());
}

@Override
public Money divide(Money value, Money divider) {
return new Money(value.getAmount() / divider.getAmount());
}

@Override
public Money power(Money value, Money exponent) {
throw new UnsupportedOperationException();
}
}));

Comparators.registerComparator(Money.class, Money.class, new Comparator<Money, Money>() {
Expand Down

0 comments on commit ffd14ae

Please sign in to comment.