View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.perf.impl;
33  
34  import java.io.Serializable;
35  import javax.annotation.Nonnull;
36  import javax.annotation.concurrent.Immutable;
37  
38  /**
39   * this class ordering is based on start Interval ordering
40   */
41  @Immutable
42  public final class Quanta implements Comparable<Quanta>, Serializable {
43  
44      private static final long serialVersionUID = 1L;
45  
46      private final long intervalStart;
47      private final long intervalEnd;
48  
49      public Quanta(final long intervalStart, final long intervalEnd) {
50          this.intervalStart = intervalStart;
51          this.intervalEnd = intervalEnd;
52      }
53  
54      public Quanta(@Nonnull final String stringVariant) {
55          int undLocation = stringVariant.indexOf('_');
56          if (undLocation < 0) {
57              throw new IllegalArgumentException("Invalid Quanta DataSource " + stringVariant);
58          }
59          String startStr = stringVariant.substring(1, undLocation);
60          String endStr = stringVariant.substring(undLocation + 1);
61          if ("NI".equals(startStr)) {
62              this.intervalStart = Long.MIN_VALUE;
63          } else {
64              this.intervalStart = Long.parseLong(startStr);
65          }
66          if ("PI".equals(endStr)) {
67              this.intervalEnd = Long.MAX_VALUE;
68          } else {
69              this.intervalEnd = Long.parseLong(endStr);
70          }
71      }
72  
73      public long getIntervalEnd() {
74          return intervalEnd;
75      }
76  
77      public long getIntervalStart() {
78          return intervalStart;
79      }
80  
81      public long getClosestToZero() {
82          return (intervalStart < 0) ? intervalEnd : intervalStart;
83      }
84  
85      @Override
86      public String toString() {
87          StringBuilder result = new StringBuilder(16);
88          result.append('Q');
89          if (intervalStart == Long.MIN_VALUE) {
90              result.append("NI");
91          } else {
92              result.append(intervalStart);
93          }
94          result.append('_');
95          if (intervalEnd == Long.MAX_VALUE) {
96              result.append("PI");
97          } else {
98              result.append(intervalEnd);
99          }
100         return result.toString();
101     }
102 
103     @Override
104     public int compareTo(final Quanta o) {
105         if (this.intervalStart < o.intervalStart) {
106             return -1;
107         } else if (this.intervalStart > o.intervalStart) {
108             return 1;
109         } else {
110             if (this.intervalEnd < o.intervalEnd) {
111                 return -1;
112             } else if (this.intervalEnd > o.intervalEnd) {
113                 return 1;
114             } else {
115                 return 0;
116             }
117         }
118     }
119 
120     @Override
121     public int hashCode() {
122         int hash = 7;
123         hash = 89 * hash + (int) (this.intervalStart ^ (this.intervalStart >>> 32));
124         return 89 * hash + (int) (this.intervalEnd ^ (this.intervalEnd >>> 32));
125     }
126 
127     @Override
128     public boolean equals(final Object obj) {
129         if (obj == null) {
130             return false;
131         }
132         if (getClass() != obj.getClass()) {
133             return false;
134         }
135         final Quanta other = (Quanta) obj;
136         return this.compareTo(other) == 0;
137     }
138 
139 }