Skip to content

Commit

Permalink
Moved common lambdas to methods so that a method reference could be r…
Browse files Browse the repository at this point in the history
…e-used (reduce code in jar)
  • Loading branch information
jdereg committed Jan 14, 2024
1 parent 154b091 commit 30cd6c2
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

import java.util.concurrent.atomic.AtomicBoolean;

/**
* @author John DeRegnaucourt ([email protected])
* Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
public class BooleanConversion {
public static Byte toByte(Object from, Converter converter, ConverterOptions options) {
Boolean b = (Boolean) from;
Expand All @@ -18,12 +36,16 @@ public static Integer toInteger(Object from, Converter converter, ConverterOptio
return b.booleanValue() ? CommonValues.INTEGER_ONE : CommonValues.INTEGER_ZERO;
}


public static Long toLong(Object from, Converter converter, ConverterOptions options) {
Boolean b = (Boolean) from;
return b.booleanValue() ? CommonValues.LONG_ONE : CommonValues.LONG_ZERO;
}

public static AtomicBoolean numberToAtomicBoolean(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return new AtomicBoolean(number.longValue() != 0);
}

public static Byte atomicToByte(Object from, Converter converter, ConverterOptions options) {
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.BYTE_ONE : CommonValues.BYTE_ZERO;
Expand Down Expand Up @@ -68,6 +90,4 @@ public static Double atomicToDouble(Object from, Converter converter, ConverterO
AtomicBoolean b = (AtomicBoolean) from;
return b.get() ? CommonValues.DOUBLE_ONE : CommonValues.DOUBLE_ZERO;
}


}
20 changes: 17 additions & 3 deletions src/main/java/com/cedarsoftware/util/convert/CommonValues.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
public class CommonValues {
public static final Byte BYTE_ZERO = (byte) 0;
public static final Byte BYTE_ONE = (byte) 1;
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/cedarsoftware/util/convert/Convert.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
package com.cedarsoftware.util.convert;


/**
* @author John DeRegnaucourt ([email protected])
* Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
@FunctionalInterface
public interface Convert<T> {
T convert(Object from, Converter converter, ConverterOptions options);
Expand Down
232 changes: 109 additions & 123 deletions src/main/java/com/cedarsoftware/util/convert/Converter.java

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/main/java/com/cedarsoftware/util/convert/ConverterOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
import java.util.Locale;
import java.util.TimeZone;

/**
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
public interface ConverterOptions {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
public class DefaultConverterOptions implements ConverterOptions {

private final Map<String, Object> customOptions;
Expand Down
76 changes: 71 additions & 5 deletions src/main/java/com/cedarsoftware/util/convert/NumberConversion.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
package com.cedarsoftware.util.convert;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
public class NumberConversion {

public static byte toByte(Object from, Converter converter, ConverterOptions options) {
Expand All @@ -28,10 +54,6 @@ public static double toDouble(Object from, Converter converter, ConverterOptions
return ((Number) from).doubleValue();
}

public static double toDoubleZero(Object from, Converter converter, ConverterOptions options) {
return 0.0d;
}

public static BigDecimal longToBigDecimal(Object from, Converter converter, ConverterOptions options) {
return BigDecimal.valueOf(((Number) from).longValue());
}
Expand Down Expand Up @@ -69,5 +91,49 @@ public static char numberToCharacter(Number number) {
public static char numberToCharacter(Object from, Converter converter, ConverterOptions options) {
return numberToCharacter((Number) from);
}
}

public static AtomicInteger numberToAtomicInteger(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return new AtomicInteger(number.intValue());
}

public static AtomicLong numberToAtomicLong(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return new AtomicLong(number.longValue());
}

public static Date numberToDate(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return new Date(number.longValue());
}

public static java.sql.Date numberToSqlDate(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return new java.sql.Date(number.longValue());
}

public static Timestamp numberToTimestamp(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return new Timestamp(number.longValue());
}

public static Calendar numberToCalendar(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return Converter.initCal(number.longValue());
}

public static LocalDate numberToLocalDate(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return LocalDate.ofEpochDay(number.longValue());
}

public static LocalDateTime numberToLocalDateTime(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return Instant.ofEpochMilli(number.longValue()).atZone(options.getSourceZoneId()).toLocalDateTime();
}

public static ZonedDateTime numberToZonedDateTime(Object from, Converter converter, ConverterOptions options) {
Number number = (Number) from;
return Instant.ofEpochMilli(number.longValue()).atZone(options.getSourceZoneId());
}
}
29 changes: 17 additions & 12 deletions src/main/java/com/cedarsoftware/util/convert/VoidConversion.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.cedarsoftware.util.convert;

/**
* @author Kenny Partlow ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* 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
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* 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.
*/
public class VoidConversion {

private static final Byte ZERO = (byte) 0;

public static Object toNull(Object from, Converter converter, ConverterOptions options) {
return null;
}

public static Boolean toBoolean(Object from, Converter converter, ConverterOptions options) {
return Boolean.FALSE;
}

public static Object toInt(Object from, Converter converter, ConverterOptions options) {
return ZERO;
}

}

0 comments on commit 30cd6c2

Please sign in to comment.