summaryrefslogtreecommitdiffstats
path: root/gerrit-common/src/main/java/com/google/gerrit/common/data/PermissionRange.java
blob: 8876c029c39baa1924610fae731bcf9c73a2cdc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.common.data;

import java.util.ArrayList;
import java.util.List;

public class PermissionRange implements Comparable<PermissionRange> {
  public static class WithDefaults extends PermissionRange {
    protected int defaultMin;
    protected int defaultMax;

    protected WithDefaults() {}

    public WithDefaults(String name, int min, int max, int defMin, int defMax) {
      super(name, min, max);
      setDefaultRange(defMin, defMax);
    }

    public int getDefaultMin() {
      return defaultMin;
    }

    public int getDefaultMax() {
      return defaultMax;
    }

    public void setDefaultRange(int min, int max) {
      defaultMin = min;
      defaultMax = max;
    }

    /** @return all values between {@link #getMin()} and {@link #getMax()} */
    public List<Integer> getValuesAsList() {
      ArrayList<Integer> r = new ArrayList<>(getRangeSize());
      for (int i = min; i <= max; i++) {
        r.add(i);
      }
      return r;
    }

    /** @return number of values between {@link #getMin()} and {@link #getMax()} */
    public int getRangeSize() {
      return max - min;
    }
  }

  protected String name;
  protected int min;
  protected int max;

  protected PermissionRange() {}

  public PermissionRange(String name, int min, int max) {
    this.name = name;

    if (min <= max) {
      this.min = min;
      this.max = max;
    } else {
      this.min = max;
      this.max = min;
    }
  }

  public String getName() {
    return name;
  }

  public boolean isLabel() {
    return Permission.isLabel(getName());
  }

  public String getLabel() {
    return Permission.extractLabel(getName());
  }

  public int getMin() {
    return min;
  }

  public int getMax() {
    return max;
  }

  /** True if the value is within the range. */
  public boolean contains(int value) {
    return getMin() <= value && value <= getMax();
  }

  /** Normalize the value to fit within the bounds of the range. */
  public int squash(int value) {
    return Math.min(Math.max(getMin(), value), getMax());
  }

  /** True both {@link #getMin()} and {@link #getMax()} are 0. */
  public boolean isEmpty() {
    return getMin() == 0 && getMax() == 0;
  }

  @Override
  public int compareTo(PermissionRange o) {
    return getName().compareTo(o.getName());
  }

  @Override
  public String toString() {
    StringBuilder r = new StringBuilder();
    if (getMin() < 0 && getMax() == 0) {
      r.append(getMin());
      r.append(' ');
    } else {
      if (getMin() != getMax()) {
        if (0 <= getMin()) {
          r.append('+');
        }
        r.append(getMin());
        r.append("..");
      }
      if (0 <= getMax()) {
        r.append('+');
      }
      r.append(getMax());
      r.append(' ');
    }
    return r.toString();
  }
}