• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.caliper.model;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import com.google.common.base.Objects;
22 
23 import java.io.Serializable;
24 
25 /**
26  * A magnitude with units.
27  *
28  * @author gak@google.com (Gregory Kick)
29  */
30 public class Value implements Serializable {
31   private static final long serialVersionUID = 1L;
32 
33   static final Value DEFAULT = new Value();
34 
create(double value, String unit)35   public static Value create(double value, String unit) {
36     return new Value(value, checkNotNull(unit));
37   }
38 
39   private double magnitude;
40   // TODO(gak): do something smarter than string for units
41   // TODO(gak): give guidelines for how to specify units.  E.g. s or seconds
42   private String unit;
43 
Value()44   private Value() {
45     this.magnitude = 0.0;
46     this.unit = "";
47   }
48 
Value(double value, String unit)49   private Value(double value, String unit) {
50     this.magnitude = value;
51     this.unit = unit;
52   }
53 
unit()54   public String unit() {
55     return unit;
56   }
57 
magnitude()58   public double magnitude() {
59     return magnitude;
60   }
61 
equals(Object obj)62   @Override public boolean equals(Object obj) {
63     if (obj == this) {
64       return true;
65     } else if (obj instanceof Value) {
66       Value that = (Value) obj;
67       return this.magnitude == that.magnitude
68           && this.unit.equals(that.unit);
69     } else {
70       return false;
71     }
72   }
73 
hashCode()74   @Override public int hashCode() {
75     return Objects.hashCode(magnitude, unit);
76   }
77 
toString()78   @Override public String toString() {
79     return new StringBuilder().append(magnitude).append(unit).toString();
80   }
81 }
82