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 * Long configuration option 24 */ 25 public class LongConfOption extends AbstractConfOption { 26 /** Default value */ 27 private long defaultValue; 28 29 /** 30 * Constructor 31 * 32 * @param key key 33 * @param defaultValue default value 34 * @param description configuration description 35 */ 36 public LongConfOption(String key, long defaultValue, String description) { 37 super(key, description); 38 this.defaultValue = defaultValue; 39 } 40 41 public long getDefaultValue() { 42 return defaultValue; 43 } 44 45 @Override public boolean isDefaultValue(Configuration conf) { 46 return get(conf) == defaultValue; 47 } 48 49 @Override public String getDefaultValueStr() { 50 return Long.toString(defaultValue); 51 } 52 53 @Override public ConfOptionType getType() { 54 return ConfOptionType.LONG; 55 } 56 57 /** 58 * Lookup value 59 * 60 * @param conf Configuration 61 * @return value set for key, or defaultValue 62 */ 63 public long get(Configuration conf) { 64 return conf.getLong(getKey(), defaultValue); 65 } 66 67 /** 68 * Lookup value, use passed in default value if not found. 69 * 70 * @param conf Configuration 71 * @param val default value to use 72 * @return set for key, or default value passed in 73 */ 74 public long getWithDefault(Configuration conf, long val) { 75 return conf.getLong(getKey(), val); 76 } 77 78 /** 79 * Set value for key 80 * 81 * @param conf Configuration 82 * @param value to set 83 */ 84 public void set(Configuration conf, long value) { 85 conf.setLong(getKey(), value); 86 } 87 88 /** 89 * Set value if it's not already present 90 * 91 * @param conf Configuration 92 * @param value to set 93 */ 94 public void setIfUnset(Configuration conf, long value) { 95 if (!contains(conf)) { 96 conf.setLong(getKey(), value); 97 } 98 } 99 }