Skip to content

Commit

Permalink
Config can now support Path and File types
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Dec 18, 2024
1 parent 04cfae8 commit bd6d35c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package com.fizzed.blaze.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -32,7 +35,7 @@ static public interface Impl<T> {
T convert(String value);
}

final static public Map<Class<?>,Impl> CONVERTERS = new HashMap<>();
final static public Map<Class<?>,Impl<?>> CONVERTERS = new HashMap<>();
static {
CONVERTERS.put(Integer.class, (Impl<Integer>) (String value) -> {
return Integer.valueOf(value);
Expand Down Expand Up @@ -81,6 +84,9 @@ static public interface Impl<T> {
CONVERTERS.put(short.class, CONVERTERS.get(Short.class));
CONVERTERS.put(byte.class, CONVERTERS.get(Byte.class));
CONVERTERS.put(double.class, CONVERTERS.get(Double.class));

CONVERTERS.put(Path.class, Paths::get);
CONVERTERS.put(File.class, File::new);
}

static public <T> T convert(String value, Class<T> type) {
Expand Down

This file was deleted.

62 changes: 62 additions & 0 deletions blaze-core/src/test/java/com/fizzed/blaze/util/ConverterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2015 Fizzed, Inc.
*
* 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.
*/
package com.fizzed.blaze.util;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
*
* @author joelauer
*/
public class ConverterTest {

@Test
public void convert() throws MalformedURLException {
assertThat(Converter.convert("1", Byte.class), is((byte)1));
assertThat(Converter.convert("1", byte.class), is((byte)1));

assertThat(Converter.convert("1", Short.class), is((short)1));
assertThat(Converter.convert("1", short.class), is((short)1));

assertThat(Converter.convert("1", Integer.class), is(1));
assertThat(Converter.convert("1", int.class), is(1));

assertThat(Converter.convert("1.2", Float.class), is(1.2f));
assertThat(Converter.convert("1.2", float.class), is(1.2f));

assertThat(Converter.convert("1.2", Double.class), is(1.2d));
assertThat(Converter.convert("1.2", double.class), is(1.2d));

assertThat(Converter.convert("true", Boolean.class), is(true));
assertThat(Converter.convert("false", boolean.class), is(false));

assertThat(Converter.convert("http://www.fizzed.com/", URI.class), is(URI.create("http://www.fizzed.com/")));
assertThat(Converter.convert("http://www.fizzed.com/", URL.class), is(new URL("http://www.fizzed.com/")));

assertThat(Converter.convert("./a", Path.class), is(Paths.get("./a")));
assertThat(Converter.convert("./a", File.class), is(Paths.get("./a").toFile()));
}

}

0 comments on commit bd6d35c

Please sign in to comment.