Search in sources :

Example 6 with MutableLong

use of com.helger.commons.mutable.MutableLong in project phoss-smp by phax.

the class SMPIDFactoryJDBC method readAndUpdateIDCounter.

@Override
protected long readAndUpdateIDCounter(@Nonnegative final int nReserveCount) {
    final MutableLong aReadValue = new MutableLong(0);
    final DBExecutor aExecutor = new SMPDBExecutor();
    aExecutor.performInTransaction(() -> {
        // Read existing value
        final String sExistingValue = SMPSettingsManagerJDBC.getSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID);
        final long nRead = StringParser.parseLong(sExistingValue, m_nInitialCount);
        aReadValue.set(nRead);
        // Write new value
        final long nNewValue = nRead + nReserveCount;
        SMPSettingsManagerJDBC.setSettingsValue(aExecutor, SETTINGS_KEY_LATEST_ID, Long.toString(nNewValue));
        if (LOGGER.isDebugEnabled())
            LOGGER.debug("Updated SQL ID from " + sExistingValue + " to " + nNewValue);
    });
    return aReadValue.longValue();
}
Also used : MutableLong(com.helger.commons.mutable.MutableLong) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Example 7 with MutableLong

use of com.helger.commons.mutable.MutableLong in project phoss-smp by phax.

the class SMPTransportProfileManagerJDBC method updateSMPTransportProfile.

@Nonnull
public EChange updateSMPTransportProfile(@Nullable final String sSMPTransportProfileID, @Nonnull @Nonempty final String sName, final boolean bIsDeprecated) {
    final MutableLong aUpdated = new MutableLong(-1);
    final DBExecutor aExecutor = newExecutor();
    final ESuccess eSuccess = aExecutor.performInTransaction(() -> {
        // Update existing
        final long nUpdated = aExecutor.insertOrUpdateOrDelete("UPDATE smp_tprofile SET name=?, deprecated=? WHERE id=?", new ConstantPreparedStatementDataProvider(sName, Boolean.valueOf(bIsDeprecated), sSMPTransportProfileID));
        aUpdated.set(nUpdated);
    });
    if (eSuccess.isFailure()) {
        // DB error
        AuditHelper.onAuditModifyFailure(SMPTransportProfile.OT, "update", sSMPTransportProfileID, "database-error");
        return EChange.UNCHANGED;
    }
    if (aUpdated.is0()) {
        // No such transport profile ID
        AuditHelper.onAuditModifyFailure(SMPTransportProfile.OT, "update", sSMPTransportProfileID, "no-such-id");
        return EChange.UNCHANGED;
    }
    AuditHelper.onAuditModifySuccess(SMPTransportProfile.OT, "update", sSMPTransportProfileID, sName, Boolean.valueOf(bIsDeprecated));
    return EChange.CHANGED;
}
Also used : ESuccess(com.helger.commons.state.ESuccess) MutableLong(com.helger.commons.mutable.MutableLong) DBExecutor(com.helger.db.jdbc.executor.DBExecutor) ConstantPreparedStatementDataProvider(com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider) Nonnull(javax.annotation.Nonnull)

Example 8 with MutableLong

use of com.helger.commons.mutable.MutableLong in project ph-commons by phax.

the class MutableTypeConverterRegistrar method registerTypeConverter.

public void registerTypeConverter(@Nonnull final ITypeConverterRegistry aRegistry) {
    // MutableBigDecimal
    aRegistry.registerTypeConverter(MutableBigDecimal.class, BigDecimal.class, MutableBigDecimal::getAsBigDecimal);
    aRegistry.registerTypeConverter(BigDecimal.class, MutableBigDecimal.class, MutableBigDecimal::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableBigDecimal.class, MutableBigDecimal::getAsBigDecimal);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableBigDecimal.class, aSource -> new MutableBigDecimal(TypeConverter.convert(aSource, BigDecimal.class)));
    // MutableBigInteger
    aRegistry.registerTypeConverter(MutableBigInteger.class, BigInteger.class, MutableBigInteger::getAsBigInteger);
    aRegistry.registerTypeConverter(BigInteger.class, MutableBigInteger.class, MutableBigInteger::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableBigInteger.class, MutableBigInteger::getAsBigInteger);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableBigInteger.class, aSource -> new MutableBigInteger(TypeConverter.convert(aSource, BigInteger.class)));
    // MutableBoolean
    aRegistry.registerTypeConverter(MutableBoolean.class, Boolean.class, MutableBoolean::getAsBoolean);
    aRegistry.registerTypeConverter(Boolean.class, MutableBoolean.class, MutableBoolean::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableBoolean.class, MutableBoolean::getAsBoolean);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableBoolean.class, aSource -> new MutableBoolean(TypeConverter.convert(aSource, Boolean.class)));
    // MutableByte
    aRegistry.registerTypeConverter(MutableByte.class, Byte.class, MutableByte::getAsByte);
    aRegistry.registerTypeConverter(Byte.class, MutableByte.class, MutableByte::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableByte.class, MutableByte::getAsByte);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableByte.class, aSource -> new MutableByte(TypeConverter.convert(aSource, Byte.class)));
    // MutableChar
    aRegistry.registerTypeConverter(MutableChar.class, Character.class, MutableChar::getAsCharacter);
    aRegistry.registerTypeConverter(Character.class, MutableChar.class, MutableChar::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableChar.class, MutableChar::getAsCharacter);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableChar.class, aSource -> new MutableChar(TypeConverter.convert(aSource, Character.class)));
    // MutableDouble
    aRegistry.registerTypeConverter(MutableDouble.class, Double.class, MutableDouble::getAsDouble);
    aRegistry.registerTypeConverter(Double.class, MutableDouble.class, MutableDouble::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableDouble.class, MutableDouble::getAsDouble);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableDouble.class, aSource -> new MutableDouble(TypeConverter.convert(aSource, Double.class)));
    // MutableFloat
    aRegistry.registerTypeConverter(MutableFloat.class, Float.class, MutableFloat::getAsFloat);
    aRegistry.registerTypeConverter(Float.class, MutableFloat.class, MutableFloat::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableFloat.class, MutableFloat::getAsFloat);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableFloat.class, aSource -> new MutableFloat(TypeConverter.convert(aSource, Float.class)));
    // MutableInt
    aRegistry.registerTypeConverter(MutableInt.class, Integer.class, MutableInt::getAsInteger);
    aRegistry.registerTypeConverter(Integer.class, MutableInt.class, MutableInt::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableInt.class, MutableInt::getAsInteger);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableInt.class, aSource -> new MutableInt(TypeConverter.convert(aSource, Integer.class)));
    // MutableLong
    aRegistry.registerTypeConverter(MutableLong.class, Long.class, MutableLong::getAsLong);
    aRegistry.registerTypeConverter(Long.class, MutableLong.class, MutableLong::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableLong.class, MutableLong::getAsLong);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableLong.class, aSource -> new MutableLong(TypeConverter.convert(aSource, Long.class)));
    // MutableShort
    aRegistry.registerTypeConverter(MutableShort.class, Short.class, MutableShort::getAsShort);
    aRegistry.registerTypeConverter(Short.class, MutableShort.class, MutableShort::new);
    aRegistry.registerTypeConverterRuleFixedSourceAnyDestination(MutableShort.class, MutableShort::getAsShort);
    aRegistry.registerTypeConverterRuleAnySourceFixedDestination(MutableShort.class, aSource -> new MutableShort(TypeConverter.convert(aSource, Short.class)));
}
Also used : MutableBigInteger(com.helger.commons.mutable.MutableBigInteger) MutableChar(com.helger.commons.mutable.MutableChar) MutableFloat(com.helger.commons.mutable.MutableFloat) MutableLong(com.helger.commons.mutable.MutableLong) MutableByte(com.helger.commons.mutable.MutableByte) MutableDouble(com.helger.commons.mutable.MutableDouble) MutableBoolean(com.helger.commons.mutable.MutableBoolean) MutableInt(com.helger.commons.mutable.MutableInt) MutableBigDecimal(com.helger.commons.mutable.MutableBigDecimal) MutableShort(com.helger.commons.mutable.MutableShort)

Example 9 with MutableLong

use of com.helger.commons.mutable.MutableLong in project ph-commons by phax.

the class StreamHelperTest method testCopyReaderToWriter.

@Test
@SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION")
public void testCopyReaderToWriter() {
    final String sInput = "Hallo";
    NonBlockingStringReader aBAIS = new NonBlockingStringReader(sInput);
    final NonBlockingStringWriter aBAOS = new NonBlockingStringWriter();
    assertTrue(StreamHelper.copyReaderToWriter(aBAIS, aBAOS).isSuccess());
    assertEquals(sInput, aBAOS.getAsString());
    // try with null streams
    aBAIS = new NonBlockingStringReader(sInput);
    assertTrue(StreamHelper.copyReaderToWriter(aBAIS, null).isFailure());
    assertTrue(StreamHelper.copyReaderToWriter(null, aBAOS).isFailure());
    aBAIS = new NonBlockingStringReader(sInput);
    assertTrue(StreamHelper.copyReaderToWriter(aBAIS, aBAOS, new char[10]).isSuccess());
    final MutableLong aML = new MutableLong(0);
    aBAIS = new NonBlockingStringReader(sInput);
    assertTrue(StreamHelper.copyReaderToWriter(aBAIS, false, aBAOS, false, new char[10], null, null, aML).isSuccess());
    assertEquals(aML.longValue(), sInput.length());
    // Must be a ByteArrayReader so that an IOException can be thrown!
    assertTrue(StreamHelper.copyReaderToWriter(new WrappedReader(aBAIS) {

        @Override
        public int read(final char[] aBuf, final int nOfs, final int nLen) throws IOException {
            throw new MockIOException();
        }
    }, aBAOS).isFailure());
    // null buffer
    StreamHelper.copyReaderToWriter(aBAIS, aBAOS, (char[]) null);
    // empty buffer
    StreamHelper.copyReaderToWriter(aBAIS, aBAOS, new char[0]);
}
Also used : MutableLong(com.helger.commons.mutable.MutableLong) MockIOException(com.helger.commons.exception.mock.MockIOException) MockIOException(com.helger.commons.exception.mock.MockIOException) IOException(java.io.IOException) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

MutableLong (com.helger.commons.mutable.MutableLong)9 DBExecutor (com.helger.db.jdbc.executor.DBExecutor)3 Nonnull (javax.annotation.Nonnull)3 Test (org.junit.Test)3 MockIOException (com.helger.commons.exception.mock.MockIOException)2 MutableBoolean (com.helger.commons.mutable.MutableBoolean)2 MutableByte (com.helger.commons.mutable.MutableByte)2 MutableChar (com.helger.commons.mutable.MutableChar)2 MutableDouble (com.helger.commons.mutable.MutableDouble)2 MutableFloat (com.helger.commons.mutable.MutableFloat)2 MutableInt (com.helger.commons.mutable.MutableInt)2 MutableShort (com.helger.commons.mutable.MutableShort)2 ESuccess (com.helger.commons.state.ESuccess)2 ConstantPreparedStatementDataProvider (com.helger.db.jdbc.callback.ConstantPreparedStatementDataProvider)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 IOException (java.io.IOException)2 IHasInputStream (com.helger.commons.io.IHasInputStream)1 MutableBigDecimal (com.helger.commons.mutable.MutableBigDecimal)1 MutableBigInteger (com.helger.commons.mutable.MutableBigInteger)1 SMPDBExecutor (com.helger.phoss.smp.backend.sql.SMPDBExecutor)1