- Notifications
You must be signed in to change notification settings - Fork 44
Config add getBoolean, getInt, getLong, getDouble, getString #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
7837ceb 236b89f 70ec30c bba66ef cf3361d File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -86,6 +86,64 @@ public <R> R get(TypedOption<?, R> option) { | |
| return this.allConfig.get(option); | ||
| } | ||
| | ||
| public boolean getBoolean(String key, boolean defaultValue) { | ||
| String value = this.allConfig.getString(key); | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else { | ||
| return Boolean.parseBoolean(value); | ||
| ||
| } | ||
| } | ||
| | ||
| public int getInt(String key, int defaultValue) { | ||
| String value = this.allConfig.getString(key); | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else { | ||
| try { | ||
| return Integer.parseInt(value); | ||
| } catch (Exception e) { | ||
| throw new ComputerException( | ||
| "Can't parse int value from '%s' for key '%s'", | ||
| value, key); | ||
| } | ||
| } | ||
| } | ||
| | ||
| public long getLong(String key, long defaultValue) { | ||
| String value = this.allConfig.getString(key); | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else { | ||
| try { | ||
| return Long.parseLong(value); | ||
| } catch (Exception e) { | ||
| throw new ComputerException( | ||
| "Can't parse long value from '%s' for key '%s'", | ||
| value, key); | ||
| } | ||
| } | ||
| } | ||
| | ||
| public double getDouble(String key, double defaultValue) { | ||
| String value = this.allConfig.getString(key); | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } else { | ||
| try { | ||
| return Double.parseDouble(value); | ||
| } catch (Exception e) { | ||
| throw new ComputerException( | ||
| "Can't parse double value from '%s' for key '%s'", | ||
| value, key); | ||
| } | ||
| } | ||
| } | ||
| | ||
| public String getString(String key, String defaultValue) { | ||
| return this.allConfig.getString(key, defaultValue); | ||
| } | ||
| | ||
| public String algorithmName() { | ||
| return this.hotConfig.algorithmName(); | ||
| } | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * 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 com.baidu.hugegraph.computer.core.config; | ||
| | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| | ||
| import org.junit.Test; | ||
| | ||
| import com.baidu.hugegraph.computer.core.common.exception.ComputerException; | ||
| import com.baidu.hugegraph.testutil.Assert; | ||
| | ||
| public class ConfigTest { | ||
| | ||
| private static final String KEY = "algorithm.page_rank.key"; | ||
| private static final String KEY_TRUE = "algorithm.page_rank.key_true"; | ||
| private static final String KEY_FALSE = "algorithm.page_rank.key_false"; | ||
| private static final String KEY_MAX = "algorithm.page_rank.key_max"; | ||
| private static final String KEY_MIN = "algorithm.page_rank.key_min"; | ||
| private static final String KEY_EMPTY = "algorithm.page_rank.no_key"; | ||
| private static final String KEY_ABC = "algorithm.page_rank.abc"; | ||
| private static final String VALUE_ABC = "abc"; | ||
| | ||
| @Test | ||
| public void testGetBoolean() { | ||
| final boolean defaultValue = false; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_TRUE, Boolean.toString(Boolean.TRUE)); | ||
| options.put(KEY_FALSE, Boolean.toString(Boolean.FALSE)); | ||
| Config config = new Config(options); | ||
| Assert.assertTrue(config.getBoolean(KEY_TRUE, defaultValue)); | ||
| Assert.assertFalse(config.getBoolean(KEY_FALSE, defaultValue)); | ||
| Assert.assertFalse(config.getBoolean(KEY_EMPTY, defaultValue)); | ||
| Assert.assertFalse(config.getBoolean(KEY_ABC, Boolean.TRUE)); | ||
| } | ||
| | ||
| @Test | ||
| public void testGetInt() { | ||
| final int defaultValue = 1; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_MAX, Integer.toString(Integer.MAX_VALUE)); | ||
| options.put(KEY_MIN, Integer.toString(Integer.MIN_VALUE)); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(Integer.MAX_VALUE, | ||
| config.getInt(KEY_MAX, defaultValue)); | ||
| Assert.assertEquals(Integer.MIN_VALUE, | ||
| config.getInt(KEY_MIN, defaultValue)); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getInt(KEY_EMPTY, defaultValue)); | ||
| Assert.assertThrows(ComputerException.class, () -> { | ||
| config.getInt(KEY_ABC, defaultValue); | ||
| } ,e -> { | ||
| Assert.assertContains("Can't parse int value", e.getMessage()); | ||
| }); | ||
| } | ||
| | ||
| @Test | ||
| public void testGetLong() { | ||
| final long defaultValue = 1L; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_MAX, Long.toString(Long.MAX_VALUE)); | ||
| options.put(KEY_MIN, Long.toString(Long.MIN_VALUE)); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(Long.MAX_VALUE, | ||
| config.getLong(KEY_MAX, defaultValue)); | ||
| Assert.assertEquals(Long.MIN_VALUE, | ||
| config.getLong(KEY_MIN, defaultValue)); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getLong(KEY_EMPTY, defaultValue)); | ||
| Assert.assertThrows(ComputerException.class, () -> { | ||
| config.getLong(KEY_ABC, defaultValue); | ||
| } ,e -> { | ||
| Assert.assertContains("Can't parse long value", e.getMessage()); | ||
| }); | ||
| } | ||
| | ||
| @Test | ||
| public void testGetDouble() throws IOException { | ||
| final double defaultValue = 1.0D; | ||
| final double delta = 0.0D; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY_MAX, Double.toString(Double.MAX_VALUE)); | ||
| options.put(KEY_MIN, Double.toString(Double.MIN_VALUE)); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(Double.MAX_VALUE, | ||
| config.getDouble(KEY_MAX, defaultValue), | ||
| delta); | ||
| Assert.assertEquals(Double.MIN_VALUE, | ||
| config.getDouble(KEY_MIN, defaultValue), | ||
| delta); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getDouble(KEY_EMPTY, defaultValue), | ||
| delta); | ||
| Assert.assertThrows(ComputerException.class, () -> { | ||
| config.getDouble(KEY_ABC, defaultValue); | ||
| } ,e -> { | ||
| Assert.assertContains("Can't parse double value", e.getMessage()); | ||
| }); | ||
| } | ||
| | ||
| @Test | ||
| public void testString() throws IOException { | ||
| String value = "The value of string"; | ||
| final String defaultValue = "The default value of string"; | ||
| Map<String, String> options = this.initialOptions(); | ||
| options.put(KEY, value); | ||
| Config config = new Config(options); | ||
| Assert.assertEquals(value, config.getString(KEY, defaultValue)); | ||
| Assert.assertEquals(defaultValue, | ||
| config.getString(KEY_EMPTY, defaultValue)); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test pass defaultValue=null | ||
| } | ||
| | ||
| private Map<String, String> initialOptions() { | ||
| Map<String, String> options = new HashMap<>(); | ||
| options.put(ComputerOptions.ALGORITHM_NAME.name(), "page_rank"); | ||
| options.put(ComputerOptions.VALUE_NAME.name(), "rank"); | ||
| options.put(ComputerOptions.EDGES_NAME.name(), "value"); | ||
| options.put(ComputerOptions.VALUE_TYPE.name(), "LONG"); | ||
| options.put(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES.name(), "false"); | ||
| options.put(ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES.name(), | ||
| "false"); | ||
| options.put(ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES.name(), | ||
| "false"); | ||
| options.put(KEY_ABC, VALUE_ABC); | ||
| return options; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright 2017 HugeGraph Authors | ||
| * | ||
| * 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 com.baidu.hugegraph.computer.core.config; | ||
| | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Suite; | ||
| | ||
| @RunWith(Suite.class) | ||
| @Suite.SuiteClasses({ | ||
| ConfigTest.class | ||
| }) | ||
| public class ConfigTestSuite { | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also update PR title