Skip to content

Commit

Permalink
feat: add converters for standard objects
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Dec 26, 2022
1 parent 6312294 commit f6a937b
Show file tree
Hide file tree
Showing 32 changed files with 2,416 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,47 @@
import org.apache.deltaspike.core.api.config.ConfigResolver;
import org.apache.deltaspike.core.api.config.ConfigSnapshot;
import org.apache.deltaspike.core.api.projectstage.ProjectStage;
import org.apache.deltaspike.core.impl.config.converter.CharacterConverter;
import org.apache.deltaspike.core.impl.config.converter.ColorConverter;
import org.apache.deltaspike.core.impl.config.converter.DimensionConverter;
import org.apache.deltaspike.core.impl.config.converter.DurationConverter;
import org.apache.deltaspike.core.impl.config.converter.EnumConverter;
import org.apache.deltaspike.core.impl.config.converter.FileConverter;
import org.apache.deltaspike.core.impl.config.converter.InetAddressConverter;
import org.apache.deltaspike.core.impl.config.converter.InstantConverter;
import org.apache.deltaspike.core.impl.config.converter.LocaleConverter;
import org.apache.deltaspike.core.impl.config.converter.PatternConverter;
import org.apache.deltaspike.core.impl.config.converter.PeriodConverter;
import org.apache.deltaspike.core.impl.config.converter.PointConverter;
import org.apache.deltaspike.core.impl.config.converter.UriConverter;
import org.apache.deltaspike.core.impl.config.converter.UrlConverter;
import org.apache.deltaspike.core.impl.config.converter.UuidConverter;
import org.apache.deltaspike.core.spi.config.ConfigSource;
import org.apache.deltaspike.core.util.ClassUtils;
import org.apache.deltaspike.core.util.ExceptionUtils;
import org.apache.deltaspike.core.util.ProjectStageProducer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.io.File;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.Period;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.util.regex.Pattern;

public class TypedResolverImpl<T> implements ConfigResolver.UntypedResolver<T>
{
Expand Down Expand Up @@ -545,6 +572,66 @@ else if (Double.class.equals(configEntryType))
{
result = Double.parseDouble(value);
}
else if (Character.class.equals(configEntryType))
{
result = new CharacterConverter().convert(value);
}
else if (Color.class.equals(configEntryType))
{
result = new ColorConverter().convert(value);
}
else if (Dimension.class.equals(configEntryType))
{
result = new DimensionConverter().convert(value);
}
else if (Duration.class.equals(configEntryType))
{
result = new DurationConverter().convert(value);
}
else if (Enum.class.equals(configEntryType))
{
result = new EnumConverter().convert(value);
}
else if (File.class.equals(configEntryType))
{
result = new FileConverter().convert(value);
}
else if (InetAddress.class.equals(configEntryType))
{
result = new InetAddressConverter().convert(value);
}
else if (Instant.class.equals(configEntryType))
{
result = new InstantConverter().convert(value);
}
else if (Locale.class.equals(configEntryType))
{
result = new LocaleConverter().convert(value);
}
else if (Pattern.class.equals(configEntryType))
{
result = new PatternConverter().convert(value);
}
else if (Period.class.equals(configEntryType))
{
result = new PeriodConverter().convert(value);
}
else if (Point.class.equals(configEntryType))
{
result = new PointConverter().convert(value);
}
else if (URI.class.equals(configEntryType))
{
result = new UriConverter().convert(value);
}
else if (URL.class.equals(configEntryType))
{
result = new UrlConverter().convert(value);
}
else if (UUID.class.equals(configEntryType))
{
result = new UuidConverter().convert(value);
}

return (T) result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.deltaspike.core.impl.config.converter;

import org.apache.deltaspike.core.api.config.ConfigResolver;

import java.util.Objects;

/**
* Converts a character or numeric value representing a character into a
* Java {@link Character} object.
*
* <p>
* This converter may also accept hexadecimal {@link String}s if obtaining
* a character from it's numeric value is desired.
*
* Intended for cases where there are concerns regarding the environment,
* such as system/file encodings between clients, applications, and servers.
* </p>
*
* @since 1.9.6
*/
public class CharacterConverter implements ConfigResolver.Converter<Character>
{

/** Determines if an input is a hexadecimal {@link String}. */
private static final String HEX_PREFIX = "0x";

/**
* @param value The input to convert.
* @return A {@link Character} which represents the value.
* @throws NullPointerException If the value is null.
* @throws IllegalArgumentException If an empty string is provided as the value.
* @throws NumberFormatException If a hexadecimal {@link String} is provided, but
* can not be parsed as an {@link Integer}.
*/
@Override
public Character convert(String value)
{
Objects.requireNonNull(value, "Value must not be null.");

if (value.isEmpty())
{
throw new IllegalArgumentException("Value must not be empty.");
}

if (value.length() == 1)
{
return value.charAt(0);
}

if (value.substring(0, 2).equalsIgnoreCase(HEX_PREFIX))
{
final String substring = value.substring(HEX_PREFIX.length());
final int hex = Integer.parseInt(substring, 16);
return (char)hex;
}

throw new IllegalArgumentException("Value can't be represented as a character.");
}
}
Loading

0 comments on commit f6a937b

Please sign in to comment.