Here you can find the source of toPrimitive(final Character value, final char defaultValue)
Parameter | Description |
---|---|
value | the Character value |
defaultValue | the default value |
public static char toPrimitive(final Character value, final char defaultValue)
//package com.java2s; /*-// ww w .ja v a2 s.com 8 * #%L * utils-commons * %% * Copyright (C) 2016 - 2018 Gilles Landel * %% * 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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. * #L% */ public class Main { /** * Get the primitive value of an {@link Integer}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * Integer value = new Integer(1); * int value1 = ObjectUtils.toPrimitive(value, 0); // => value1 = 1 * int value2 = ObjectUtils.toPrimitive((Integer) null, 0); // => value2 = 0 * </pre> * * @param value * the {@link Integer} value * @param defaultValue * the default value * @return a primitive int */ public static int toPrimitive(final Integer value, final int defaultValue) { if (value == null) { return defaultValue; } else { return value.intValue(); } } /** * Get the primitive value of an {@link Long}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * Long value = new Long(1l); * long value1 = ObjectUtils.toPrimitive(value, 0l); // => value1 = 1l * long value2 = ObjectUtils.toPrimitive((Long) null, 0l); // => value2 = 0l * </pre> * * @param value * the {@link Long} value * @param defaultValue * the default value * @return a primitive long */ public static long toPrimitive(final Long value, final long defaultValue) { if (value == null) { return defaultValue; } else { return value.longValue(); } } /** * Get the primitive value of an {@link Float}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * Float value = new Float(1f); * float value1 = ObjectUtils.toPrimitive(value, 0f); // => value1 = 1f * float value2 = ObjectUtils.toPrimitive((Float) null, 0f); // => value2 = 0f * </pre> * * @param value * the {@link Float} value * @param defaultValue * the default value * @return a primitive float */ public static float toPrimitive(final Float value, final float defaultValue) { if (value == null) { return defaultValue; } else { return value.floatValue(); } } /** * Get the primitive value of an {@link Double}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * Double value = new Double(1d); * double value1 = ObjectUtils.toPrimitive(value, 0); // => value1 = 1d * double value2 = ObjectUtils.toPrimitive((Double) null, 0); // => value2 = 0d * </pre> * * @param value * the {@link Double} value * @param defaultValue * the default value * @return a primitive double */ public static double toPrimitive(final Double value, final double defaultValue) { if (value == null) { return defaultValue; } else { return value.doubleValue(); } } /** * Get the primitive value of an {@link Short}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * Short value0 = new Short((short) 1); * short value1 = ObjectUtils.toPrimitive(value0, (short) 0); // => value1 = 1 * short value2 = ObjectUtils.toPrimitive((Short) null, (short) 0); // => value2 = 0 * </pre> * * @param value * the {@link Short} value * @param defaultValue * the default value * @return a primitive short */ public static short toPrimitive(final Short value, final short defaultValue) { if (value == null) { return defaultValue; } else { return value.shortValue(); } } /** * Get the primitive value of an {@link Byte}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * Byte one = new Byte((byte) 1); * byte value1 = ObjectUtils.toPrimitive(one, (byte) 0); // => value1 = 1 * byte value2 = ObjectUtils.toPrimitive((Byte) null, (byte) 0); // => value2 = 0 * </pre> * * @param value * the {@link Byte} value * @param defaultValue * the default value * @return a primitive byte */ public static byte toPrimitive(final Byte value, final byte defaultValue) { if (value == null) { return defaultValue; } else { return value.byteValue(); } } /** * Get the primitive value of an {@link Character}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * Character space = new Character((char) 32); * char value1 = ObjectUtils.toPrimitive(space, '_'); // => value1 = ' ' * char value2 = ObjectUtils.toPrimitive((Character) null, '_'); // => value2 = '_' * </pre> * * @param value * the {@link Character} value * @param defaultValue * the default value * @return a primitive char */ public static char toPrimitive(final Character value, final char defaultValue) { if (value == null) { return defaultValue; } else { return value.charValue(); } } /** * Get the primitive value of an {@link Boolean}. If {@code value} is not * {@code null}, returns the primitive value of {@code value} otherwise returns * {@code defaultValue} * * <pre> * boolean value1 = ObjectUtils.toPrimitive(Boolean.TRUE, false); // => value1 = true * boolean value2 = ObjectUtils.toPrimitive(Boolean.FALSE, true); // => value2 = false * boolean value3 = ObjectUtils.toPrimitive(null, false); // => value3 = false * </pre> * * @param value * the {@link Boolean} value * @param defaultValue * the default value * @return a primitive boolean * * @see BooleanUtils#isFalse(Boolean) * @see BooleanUtils#isTrue(Boolean) * @see BooleanUtils#isNotFalse(Boolean) * @see BooleanUtils#isNotTrue(Boolean) */ public static boolean toPrimitive(final Boolean value, final boolean defaultValue) { if (value == null) { return defaultValue; } else { return value.booleanValue(); } } }