Search in sources :

Example 41 with EChange

use of com.helger.commons.state.EChange in project ph-commons by phax.

the class ICommonsCollection method addAllMapped.

/**
 * Add all passed elements after performing a mapping using the provided
 * function.
 *
 * @param aElements
 *        The elements to be added after mapping. May be <code>null</code>.
 * @param aMapper
 *        The mapping function to be executed for all provided elements. May
 *        not be <code>null</code>.
 * @return {@link EChange#CHANGED} if at least one element was added,
 *         {@link EChange#UNCHANGED}. Never <code>null</code>.
 * @param <SRCTYPE>
 *        The source type to be mapped from
 */
@Nonnull
default <SRCTYPE> EChange addAllMapped(@Nullable final SRCTYPE[] aElements, @Nonnull final Function<? super SRCTYPE, ? extends ELEMENTTYPE> aMapper) {
    ValueEnforcer.notNull(aMapper, "Mapper");
    EChange eChange = EChange.UNCHANGED;
    if (aElements != null)
        for (final SRCTYPE aValue : aElements) eChange = eChange.or(add(aMapper.apply(aValue)));
    return eChange;
}
Also used : EChange(com.helger.commons.state.EChange) Nonnull(javax.annotation.Nonnull)

Example 42 with EChange

use of com.helger.commons.state.EChange in project ph-commons by phax.

the class ICommonsCollection method addAllMapped.

/**
 * Add all passed elements matching the provided filter after performing a
 * mapping using the provided function.
 *
 * @param aElements
 *        The elements to be added after mapping. May be <code>null</code>.
 * @param aMapper
 *        The mapping function to be executed for all provided elements. May
 *        not be <code>null</code>.
 * @param aFilter
 *        The filter to be applied on the mapped element. May be
 *        <code>null</code>.
 * @return {@link EChange#CHANGED} if at least one element was added,
 *         {@link EChange#UNCHANGED}. Never <code>null</code>.
 * @param <SRCTYPE>
 *        The source type to be mapped from
 * @since 8.5.2
 */
@Nonnull
default <SRCTYPE> EChange addAllMapped(@Nullable final Iterable<? extends SRCTYPE> aElements, @Nonnull final Function<? super SRCTYPE, ? extends ELEMENTTYPE> aMapper, @Nullable final Predicate<? super ELEMENTTYPE> aFilter) {
    ValueEnforcer.notNull(aMapper, "Mapper");
    if (aFilter == null)
        return addAllMapped(aElements, aMapper);
    EChange eChange = EChange.UNCHANGED;
    if (aElements != null)
        for (final SRCTYPE aValue : aElements) {
            final ELEMENTTYPE aMapped = aMapper.apply(aValue);
            if (aFilter.test(aMapped))
                eChange = eChange.or(add(aMapped));
        }
    return eChange;
}
Also used : EChange(com.helger.commons.state.EChange) Nonnull(javax.annotation.Nonnull)

Example 43 with EChange

use of com.helger.commons.state.EChange in project ph-commons by phax.

the class BaseTypeConverterRegistrar method registerTypeConverter.

/**
 * Register all type converters for the 15 base types:<br>
 * <ul>
 * <li>Boolean</li>
 * <li>Byte</li>
 * <li>Character</li>
 * <li>Double</li>
 * <li>Float</li>
 * <li>Integer</li>
 * <li>Long</li>
 * <li>Short</li>
 * <li>String</li>
 * <li>BigDecimal</li>
 * <li>BigInteger</li>
 * <li>AtomicBoolean</li>
 * <li>AtomicInteger</li>
 * <li>AtomicLong</li>
 * <li>StringBuffer</li>
 * <li>StringBuilder</li>
 * </ul>
 */
public void registerTypeConverter(@Nonnull final ITypeConverterRegistry aRegistry) {
    // to Boolean
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Boolean.class, aSource -> Boolean.valueOf(aSource.intValue() != 0));
    aRegistry.registerTypeConverter(Character.class, Boolean.class, aSource -> Boolean.valueOf(aSource.charValue() != 0));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Boolean.class, aSource -> StringParser.parseBoolObj(aSource, (Boolean) null));
    // to Byte
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Byte.class, aSource -> Byte.valueOf(aSource.byteValue()));
    aRegistry.registerTypeConverter(Boolean.class, Byte.class, aSource -> Byte.valueOf(aSource.booleanValue() ? (byte) 1 : (byte) 0));
    aRegistry.registerTypeConverter(Character.class, Byte.class, aSource -> Byte.valueOf((byte) aSource.charValue()));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Byte.class, aSource -> StringParser.parseByteObj(aSource, (Byte) null));
    // to Character
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Character.class, aSource -> Character.valueOf((char) aSource.intValue()));
    aRegistry.registerTypeConverter(Boolean.class, Character.class, aSource -> Character.valueOf(aSource.booleanValue() ? (char) 1 : (char) 0));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Character.class, aSource -> {
        final String sSource = aSource.toString();
        return sSource.length() == 1 ? Character.valueOf(sSource.charAt(0)) : null;
    });
    // to Double
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Double.class, aSource -> Double.valueOf(aSource.doubleValue()));
    aRegistry.registerTypeConverter(Boolean.class, Double.class, aSource -> Double.valueOf(aSource.booleanValue() ? 1d : 0d));
    aRegistry.registerTypeConverter(Character.class, Double.class, aSource -> Double.valueOf(aSource.charValue()));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Double.class, aSource -> StringParser.parseDoubleObj(aSource, (Double) null));
    // to Float
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Float.class, aSource -> Float.valueOf(aSource.floatValue()));
    aRegistry.registerTypeConverter(Boolean.class, Float.class, aSource -> Float.valueOf(aSource.booleanValue() ? 1f : 0f));
    aRegistry.registerTypeConverter(Character.class, Float.class, aSource -> Float.valueOf(aSource.charValue()));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Float.class, aSource -> StringParser.parseFloatObj(aSource, (Float) null));
    // to Integer
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Integer.class, aSource -> Integer.valueOf(aSource.intValue()));
    aRegistry.registerTypeConverter(Boolean.class, Integer.class, aSource -> Integer.valueOf(aSource.booleanValue() ? 1 : 0));
    aRegistry.registerTypeConverter(Character.class, Integer.class, aSource -> Integer.valueOf(aSource.charValue()));
    aRegistry.registerTypeConverter(String.class, Integer.class, aSource -> {
        final BigDecimal aBD = StringParser.parseBigDecimal(aSource, (BigDecimal) null);
        return aBD == null ? null : Integer.valueOf(aBD.intValue());
    });
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Integer.class, aSource -> StringParser.parseIntObj(aSource, (Integer) null));
    // to Long
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Long.class, aSource -> Long.valueOf(aSource.longValue()));
    aRegistry.registerTypeConverter(Boolean.class, Long.class, aSource -> Long.valueOf(aSource.booleanValue() ? 1L : 0L));
    aRegistry.registerTypeConverter(Character.class, Long.class, aSource -> Long.valueOf(aSource.charValue()));
    aRegistry.registerTypeConverter(String.class, Long.class, aSource -> {
        final BigDecimal aBD = StringParser.parseBigDecimal(aSource, (BigDecimal) null);
        return aBD == null ? null : Long.valueOf(aBD.longValue());
    });
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Long.class, aSource -> StringParser.parseLongObj(aSource, (Long) null));
    // to Short
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, Short.class, aSource -> Short.valueOf(aSource.shortValue()));
    aRegistry.registerTypeConverter(Boolean.class, Short.class, aSource -> Short.valueOf(aSource.booleanValue() ? (short) 1 : (short) 0));
    aRegistry.registerTypeConverter(Character.class, Short.class, aSource -> Short.valueOf((short) aSource.charValue()));
    aRegistry.registerTypeConverter(String.class, Short.class, aSource -> {
        final BigDecimal aBD = StringParser.parseBigDecimal(aSource, (BigDecimal) null);
        return aBD == null ? null : Short.valueOf(aBD.shortValue());
    });
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(Short.class, aSource -> StringParser.parseShortObj(aSource, (Short) null));
    // to String
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(String.class, Object::toString);
    // to BigDecimal
    aRegistry.registerTypeConverter(BigInteger.class, BigDecimal.class, BigDecimal::new);
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, BigDecimal.class, aSource -> BigDecimal.valueOf(aSource.doubleValue()));
    aRegistry.registerTypeConverter(Boolean.class, BigDecimal.class, aSource -> aSource.booleanValue() ? BigDecimal.ONE : BigDecimal.ZERO);
    aRegistry.registerTypeConverter(Character.class, BigDecimal.class, aSource -> BigDecimal.valueOf(aSource.charValue()));
    aRegistry.registerTypeConverter(Byte.class, BigDecimal.class, aSource -> BigDecimal.valueOf(aSource.intValue()));
    aRegistry.registerTypeConverter(Integer.class, BigDecimal.class, aSource -> BigDecimal.valueOf(aSource.intValue()));
    aRegistry.registerTypeConverter(Long.class, BigDecimal.class, aSource -> BigDecimal.valueOf(aSource.longValue()));
    aRegistry.registerTypeConverter(Short.class, BigDecimal.class, aSource -> BigDecimal.valueOf(aSource.shortValue()));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(BigDecimal.class, aSource -> StringParser.parseBigDecimal(aSource.toString(), (BigDecimal) null));
    // to BigInteger
    aRegistry.registerTypeConverter(BigDecimal.class, BigInteger.class, BigDecimal::toBigInteger);
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Number.class, BigInteger.class, aSource -> BigInteger.valueOf(aSource.longValue()));
    aRegistry.registerTypeConverter(Boolean.class, BigInteger.class, aSource -> aSource.booleanValue() ? BigInteger.ONE : BigInteger.ZERO);
    aRegistry.registerTypeConverter(Character.class, BigInteger.class, aSource -> BigInteger.valueOf(aSource.charValue()));
    aRegistry.registerTypeConverter(String.class, BigInteger.class, aSource -> {
        final BigDecimal aBD = StringParser.parseBigDecimal(aSource, (BigDecimal) null);
        return aBD == null ? null : aBD.toBigInteger();
    });
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(BigInteger.class, aSource -> StringParser.parseBigInteger(aSource.toString(), (BigInteger) null));
    // AtomicBoolean
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(AtomicBoolean.class, aSource -> Boolean.valueOf(aSource.get()));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(AtomicBoolean.class, aSource -> new AtomicBoolean(TypeConverter.convert(aSource, Boolean.class).booleanValue()));
    // AtomicInteger
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(AtomicInteger.class, aSource -> Integer.valueOf(aSource.get()));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(AtomicInteger.class, aSource -> new AtomicInteger(TypeConverter.convert(aSource, Integer.class).intValue()));
    // AtomicLong
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(AtomicLong.class, aSource -> Long.valueOf(aSource.get()));
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(AtomicLong.class, aSource -> new AtomicLong(TypeConverter.convert(aSource, Long.class).longValue()));
    // to StringBuilder
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(StringBuilder.class, aSource -> {
        if (aSource instanceof CharSequence)
            return new StringBuilder((CharSequence) aSource);
        return new StringBuilder(TypeConverter.convert(aSource, String.class));
    });
    // to StringBuffer
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(StringBuffer.class, aSource -> {
        if (aSource instanceof CharSequence)
            return new StringBuffer((CharSequence) aSource);
        return new StringBuffer(TypeConverter.convert(aSource, String.class));
    });
    // Enum
    /*
     * We need to append the Enum class name, otherwise we cannot resolve it!
     * Use the colon as it is not allowed in class names.
     */
    aRegistry.registerTypeConverterRuleAssignableSourceFixedDestination(Enum.class, String.class, aSource -> aSource.getClass().getName() + ':' + aSource.name());
    aRegistry.registerTypeConverterRuleFixedSourceAssignableDestination(String.class, Enum.class, x -> {
        /*
       * Split class name and enum value name
       */
        final ICommonsList<String> aParts = StringHelper.getExploded(':', x, 2);
        try {
            /*
         * Resolve any enum class. Note: The explicit EChange is just here,
         * because an explicit enum type is needed. It must of course not only
         * be EChange :)
         */
            final Class<EChange> aClass = GenericReflection.getClassFromName(aParts.get(0));
            /*
         * And look up the element by name
         */
            return Enum.valueOf(aClass, aParts.get(1));
        } catch (final ClassNotFoundException ex) {
            return null;
        }
    });
    // String[] to any
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(String[].class, x -> {
        if (x.length == 0)
            return null;
        if (x.length > 1) {
            LoggerFactory.getLogger("TypeConverter").warn("An array with " + x.length + " items is present; using the first value: " + Arrays.toString(x));
        }
        return x[0];
    });
}
Also used : BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) BigInteger(java.math.BigInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EChange(com.helger.commons.state.EChange)

Example 44 with EChange

use of com.helger.commons.state.EChange in project ph-commons by phax.

the class DirectedGraph method removeRelation.

@Nonnull
public EChange removeRelation(@Nullable final IMutableDirectedGraphRelation aRelation) {
    EChange ret = EChange.UNCHANGED;
    if (aRelation != null) {
        ret = ret.or(aRelation.getFrom().removeOutgoingRelation(aRelation));
        ret = ret.or(aRelation.getTo().removeIncomingRelation(aRelation));
        if (ret.isChanged())
            _invalidateCache();
    }
    return ret;
}
Also used : EChange(com.helger.commons.state.EChange) Nonnull(javax.annotation.Nonnull)

Example 45 with EChange

use of com.helger.commons.state.EChange in project ph-commons by phax.

the class MimeTypeInfoManager method clearCache.

/**
 * Remove all registered mime types
 *
 * @return {@link EChange}.
 */
@Nonnull
public EChange clearCache() {
    return m_aRWLock.writeLockedGet(() -> {
        EChange ret = m_aList.removeAll();
        if (!m_aMapExt.isEmpty()) {
            m_aMapExt.clear();
            ret = EChange.CHANGED;
        }
        if (!m_aMapMimeType.isEmpty()) {
            m_aMapMimeType.clear();
            ret = EChange.CHANGED;
        }
        return ret;
    });
}
Also used : EChange(com.helger.commons.state.EChange) Nonnull(javax.annotation.Nonnull)

Aggregations

EChange (com.helger.commons.state.EChange)50 Nonnull (javax.annotation.Nonnull)42 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)9 ISMPServiceGroup (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroup)9 ISMPServiceGroupManager (com.helger.phoss.smp.domain.servicegroup.ISMPServiceGroupManager)9 SMPServerException (com.helger.phoss.smp.exception.SMPServerException)7 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)6 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)6 IIdentifierFactory (com.helger.peppolid.factory.IIdentifierFactory)6 SMPNotFoundException (com.helger.phoss.smp.exception.SMPNotFoundException)6 ESuccess (com.helger.commons.state.ESuccess)5 IDocumentTypeIdentifier (com.helger.peppolid.IDocumentTypeIdentifier)5 ISMPServiceInformationManager (com.helger.phoss.smp.domain.serviceinfo.ISMPServiceInformationManager)5 IUser (com.helger.photon.security.user.IUser)5 Map (java.util.Map)5 ReturnsMutableObject (com.helger.commons.annotation.ReturnsMutableObject)4 CallbackList (com.helger.commons.callback.CallbackList)4 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)4 ICommonsList (com.helger.commons.collection.impl.ICommonsList)4 MutableBoolean (com.helger.commons.mutable.MutableBoolean)4