Search in sources :

Example 6 with NonNull

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;
}
Also used : SplitJoinPermission(com.icodici.universa.contract.permissions.SplitJoinPermission) NonNull(org.checkerframework.checker.nullness.qual.NonNull) java.util(java.util) FileFilter(java.io.FileFilter) Contract(com.icodici.universa.contract.Contract) Files(java.nio.file.Files) Paths(java.nio.file.Paths) Permission(com.icodici.universa.contract.permissions.Permission) IOException(java.io.IOException) Decimal(com.icodici.universa.Decimal) Path(java.nio.file.Path) File(java.io.File) Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract)

Example 7 with NonNull

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);
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) ArrayList(java.util.ArrayList) StrBuilder(org.apache.commons.lang3.text.StrBuilder)

Example 8 with NonNull

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);
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) StrBuilder(org.apache.commons.lang3.text.StrBuilder)

Example 9 with NonNull

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;
}
Also used : Binder(net.sergeych.tools.Binder) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

NonNull (org.checkerframework.checker.nullness.qual.NonNull)9 Binder (net.sergeych.tools.Binder)4 HashMap (java.util.HashMap)2 StrBuilder (org.apache.commons.lang3.text.StrBuilder)2 Decimal (com.icodici.universa.Decimal)1 Contract (com.icodici.universa.contract.Contract)1 Permission (com.icodici.universa.contract.permissions.Permission)1 SplitJoinPermission (com.icodici.universa.contract.permissions.SplitJoinPermission)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1