use of org.checkerframework.checker.nullness.qual.NonNull in project universa by UniversaBlockchain.
the class Wallet method buildContractWithValue.
/**
* Join this contract with other (split when the sum of contracts is greater) or
* split the current one to have the input value in the one contract.
*
* @param fieldName is name of field for split or join
* @param value is value to have in a one contract
* @return built {@link Contract}
* @throws Exception if something went wrong
*/
public synchronized Contract buildContractWithValue(String fieldName, @NonNull Decimal value) throws Exception {
if (value == null || Decimal.ZERO.equals(value) || this.contracts.size() == 0)
return null;
Contract result = null;
// sort here because someone could add a new contract to the list at any time
this.contracts.sort(Comparator.comparing(c -> getValue(c, fieldName)));
List<Contract> selectedContracts = new ArrayList<>();
int contractsSize = this.contracts.size();
Decimal sum = Decimal.ZERO;
for (int i = 0; i < contractsSize; i++) {
Contract contract = this.contracts.get(i);
Decimal cValue = getValue(contract, fieldName);
sum = sum.add(cValue);
if (selectedContracts.size() >= MAX_SELECTIONS_OF_CONTRACTS && selectedContracts.get(i % MAX_SELECTIONS_OF_CONTRACTS) != null) {
sum.subtract(getValue(selectedContracts.get(i % MAX_SELECTIONS_OF_CONTRACTS), fieldName));
selectedContracts.set(i % MAX_SELECTIONS_OF_CONTRACTS, contract);
} else
selectedContracts.add(i % MAX_SELECTIONS_OF_CONTRACTS, contract);
int compared = sum.compareTo(value);
if (compared == 0) {
result = joinAndRemoveFromContracts(selectedContracts);
result.getStateData().set(fieldName, sum);
break;
} else if (compared == 1) {
result = joinAndRemoveFromContracts(selectedContracts);
result.getStateData().set(fieldName, sum);
// split with change and add it back to the contracts
Contract newContract = result.splitValue(fieldName, sum.subtract(value));
this.contracts.add(newContract);
break;
}
}
if (result == null)
throw new IllegalArgumentException("The amount of contracts from the walled does not match the expected value.");
return result;
}
use of org.checkerframework.checker.nullness.qual.NonNull in project checker-framework by typetools.
the class GradleExample method main.
public static void main(final String[] args) {
System.out.println("Hello World!");
StrBuilder stb = new StrBuilder();
// error on this line
@NonNull Object nn = nullable;
System.out.println(nn);
List<@NonNull String> list = new ArrayList<>();
// error on this line
list.add(null);
}
use of org.checkerframework.checker.nullness.qual.NonNull in project checker-framework by typetools.
the class MavenExample method main.
public static void main(final String[] args) {
System.out.println("Hello World!");
StrBuilder stb = new StrBuilder();
// error on this line
@NonNull Object nn = nullable;
System.out.println(nn);
}
use of org.checkerframework.checker.nullness.qual.NonNull in project universa by UniversaBlockchain.
the class BiMapper method serialize.
/**
* Try to serialize object to {@link Binder} using current set of {@link BiAdapter}. See {@link
* #registerAdapter(Class, BiAdapter)}, {@link #registerClass(Class)} for more. This method properly serializes Maps
* and Collections, serializing it contents.
*
* @param x object to serialize (can be array, list, map, binder or any object with registered adapter. processes
* in depth, e.g. all values in the map or items in the list.
* @param <T>
*
* @return either a Binder or a simple object x (e.g. String if x instanceof String).
*
* @throws IllegalArgumentException if unkonwn ibject ecnountered which can not be serialized.
*/
@NonNull
public <T> T serialize(Object x, BiSerializer serializer) {
if (x instanceof String || x instanceof Number || x instanceof Boolean || x == null)
return (T) x;
Class<?> klass = x.getClass();
if (klass.isArray() && !(klass.getComponentType() == byte.class)) {
x = Arrays.asList((Object[]) x);
}
if (x instanceof Collection) {
return (T) ((Collection) x).stream().map(i -> serialize(i, serializer)).collect(Collectors.toList());
}
String canonicalName = klass.getCanonicalName();
BiAdapter adapter = adapters.get(canonicalName);
if (adapter == null) {
if (x instanceof Map) {
((Map) x).replaceAll((k, v) -> serialize(v, serializer));
return (T) x;
}
// just leave it as it is
return (T) x;
// throw new IllegalArgumentException("can't convert to binder " + canonicalName + ": " + x);
}
Binder result = adapter.serialize(x, serializer);
String tn = adapter.typeName();
result.put("__type", tn != null ? tn : canonicalName);
return (T) result;
}
Aggregations