This project has retired. For details please refer to its Attic page.
AbstractConfOption xref
View Javadoc

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  import org.apache.log4j.Logger;
22  
23  import com.google.common.base.Objects;
24  import com.google.common.collect.ComparisonChain;
25  
26  /**
27   * Abstract base class of configuration options
28   */
29  public abstract class AbstractConfOption
30      implements Comparable<AbstractConfOption> {
31    /** Logger */
32    private static final Logger LOG = Logger.getLogger(AbstractConfOption.class);
33  
34    /** Key for configuration */
35    private final String key;
36  
37    /** Configuration option description */
38    private final String description;
39  
40    /**
41     * Constructor
42     *
43     * @param key configuration key
44     * @param description configuration description
45     */
46    public AbstractConfOption(String key, String description) {
47      this.key = key;
48      this.description = description;
49    }
50  
51    public String getKey() {
52      return key;
53    }
54  
55    /**
56     * @return the description
57     */
58    public String getDescription() {
59      return description;
60    }
61  
62    /**
63     * Check if option is set in configuration
64     *
65     * @param conf Configuration
66     * @return true if option is set
67     */
68    public boolean contains(Configuration conf) {
69      return conf.get(key) != null;
70    }
71  
72    @Override public int compareTo(AbstractConfOption o) {
73      return ComparisonChain.start()
74        .compare(getType(), o.getType())
75        .compare(key, o.key)
76        .result();
77    }
78  
79    @Override
80    public boolean equals(Object o) {
81      if (this == o) {
82        return true;
83      }
84      if (!(o instanceof AbstractConfOption)) {
85        return false;
86      }
87  
88      AbstractConfOption that = (AbstractConfOption) o;
89      return Objects.equal(getType(), that.getType()) &&
90          Objects.equal(key, that.key);
91    }
92  
93    @Override
94    public int hashCode() {
95      return Objects.hashCode(key);
96    }
97  
98    @Override public String toString() {
99      StringBuilder sb = new StringBuilder(30);
100     sb.append("  ").append(key).append(" => ").append(getDefaultValueStr());
101     sb.append(" (").append(getType().toString().toLowerCase()).append(")\n");
102     return sb.toString();
103   }
104 
105   /**
106    * Check if the value set is the same as the default value
107    *
108    * @param conf Configuration
109    * @return true if value set is default value
110    */
111   public abstract boolean isDefaultValue(Configuration conf);
112 
113   /**
114    * Get string representation of default value
115    *
116    * @return String
117    */
118   public abstract String getDefaultValueStr();
119 
120   /**
121    * Get type this option holds
122    *
123    * @return ConfOptionType
124    */
125   public abstract ConfOptionType getType();
126 }