Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Add Modulo (%) operator #133

Open
wants to merge 1 commit into
base: 1.15-2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public final class DollarParser {
},
new DollarPart.Deserializer[] {
new ProductDollarOperator.Deserializer(),
new QuotientDollarOperator.Deserializer()
new QuotientDollarOperator.Deserializer(),
new ModuloDollarOperator.Deserializer()
},
new DollarPart.Deserializer[] {
new SumDollarOperator.Deserializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020-2022 Siphalor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied.
* See the License for the specific language governing
* permissions and limitations under the License.
*/

package de.siphalor.nbtcrafting.dollar.part.operator;

import org.apache.commons.lang3.StringUtils;

import de.siphalor.nbtcrafting.dollar.DollarDeserializationException;
import de.siphalor.nbtcrafting.dollar.DollarException;
import de.siphalor.nbtcrafting.dollar.DollarParser;
import de.siphalor.nbtcrafting.dollar.part.DollarPart;
import de.siphalor.nbtcrafting.dollar.part.ValueDollarPart;
import de.siphalor.nbtcrafting.util.NumberUtil;

public class ModuloDollarOperator extends BinaryDollarOperator {
private ModuloDollarOperator(DollarPart first, DollarPart second) {
super(first, second);
}

public static DollarPart of(DollarPart first, DollarPart second) throws DollarException {
DollarPart instance = new ModuloDollarOperator(first, second);
if (first.isConstant() && second.isConstant()) {
return ValueDollarPart.of(instance.evaluate(null));
}
return instance;
}

@Override
public Object apply(Object first, Object second) {
if (first instanceof Number && second instanceof Number) {
return NumberUtil.remainder((Number) first, (Number) second);
}
return null;
}

public static class Deserializer implements DollarPart.Deserializer {
@Override
public boolean matches(int character, DollarParser dollarParser) {
return character == '%';
}

@Override
public DollarPart parse(DollarParser dollarParser, DollarPart lastDollarPart, int priority) throws DollarDeserializationException {
dollarParser.skip();
return new ModuloDollarOperator(lastDollarPart, dollarParser.parse(priority));
}
}
}
57 changes: 39 additions & 18 deletions src/main/java/de/siphalor/nbtcrafting/util/NumberUtil.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* Copyright 2020-2022 Siphalor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied.
* See the License for the specific language governing
* permissions and limitations under the License.
*/

package de.siphalor.nbtcrafting.util;
/*
* Copyright 2020-2022 Siphalor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied.
* See the License for the specific language governing
* permissions and limitations under the License.
*/
package de.siphalor.nbtcrafting.util;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -174,4 +174,25 @@ public static Number quotient(Number a, Number b) {
return a.doubleValue() / b.doubleValue();
}
}

public static Number remainder(Number a, Number b) {
a = denullify(a);
if (b == null || b.doubleValue() == 0.0D)
return Math.signum(a.doubleValue()) * Double.POSITIVE_INFINITY;
switch (findSmallestType(a, b)) {
case 0:
case 1:
return (byte) (a.byteValue() % b.byteValue());
case 2:
return (short) (a.shortValue() % b.shortValue());
case 3:
return a.intValue() % b.intValue();
case 4:
return a.longValue() % b.longValue();
case 5:
return a.floatValue() % b.floatValue();
default:
return a.doubleValue() % b.doubleValue();
}
}
}