1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 package org.apache.giraph.conf; 19 20 import org.apache.hadoop.conf.Configuration; 21 22 /** 23 * Integer configuration option 24 */ 25 public class IntConfOption extends AbstractConfOption { 26 /** Default value */ 27 private final int defaultValue; 28 29 /** 30 * Constructor 31 * 32 * @param key key 33 * @param defaultValue default value 34 * @param description configuration description 35 */ 36 public IntConfOption(String key, int defaultValue, String description) { 37 super(key, description); 38 this.defaultValue = defaultValue; 39 } 40 41 /** 42 * Constructor 43 * 44 * @param key key 45 * @param defaultValue default value 46 * @param description configuration description 47 */ 48 public IntConfOption(String key, long defaultValue, String description) { 49 super(key, description); 50 this.defaultValue = (int) defaultValue; 51 } 52 53 public int getDefaultValue() { 54 return defaultValue; 55 } 56 57 @Override public boolean isDefaultValue(Configuration conf) { 58 return get(conf) == defaultValue; 59 } 60 61 @Override public String getDefaultValueStr() { 62 return Integer.toString(defaultValue); 63 } 64 65 @Override public ConfOptionType getType() { 66 return ConfOptionType.INTEGER; 67 } 68 69 /** 70 * Lookup value 71 * 72 * @param conf Configuration 73 * @return value for key, or default value if not set 74 */ 75 public int get(Configuration conf) { 76 return conf.getInt(getKey(), defaultValue); 77 } 78 79 /** 80 * Set value 81 * 82 * @param conf Configuration 83 * @param value to set 84 */ 85 public void set(Configuration conf, int value) { 86 conf.setInt(getKey(), value); 87 } 88 89 /** 90 * Set value if it's not already present 91 * 92 * @param conf Configuration 93 * @param value to set 94 */ 95 public void setIfUnset(Configuration conf, int value) { 96 if (!contains(conf)) { 97 conf.setInt(getKey(), value); 98 } 99 } 100 }