Line data Source code
1 : // Copyright (C) 2010 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.entities; 16 : 17 : import java.util.ArrayList; 18 : import java.util.List; 19 : 20 : /** 21 : * Represents a closed interval [min, max] with a name. The special value [0, 0] is understood to be 22 : * the empty range. 23 : */ 24 : public class PermissionRange implements Comparable<PermissionRange> { 25 : public static class WithDefaults extends PermissionRange { 26 : protected int defaultMin; 27 : protected int defaultMax; 28 : 29 0 : protected WithDefaults() {} 30 : 31 : public WithDefaults(String name, int min, int max, int defMin, int defMax) { 32 151 : super(name, min, max); 33 151 : setDefaultRange(defMin, defMax); 34 151 : } 35 : 36 : public int getDefaultMin() { 37 151 : return defaultMin; 38 : } 39 : 40 : public int getDefaultMax() { 41 151 : return defaultMax; 42 : } 43 : 44 : public void setDefaultRange(int min, int max) { 45 151 : defaultMin = min; 46 151 : defaultMax = max; 47 151 : } 48 : 49 : /** Returns all values between {@link #getMin()} and {@link #getMax()} */ 50 : public List<Integer> getValuesAsList() { 51 0 : ArrayList<Integer> r = new ArrayList<>(getRangeSize()); 52 0 : for (int i = min; i <= max; i++) { 53 0 : r.add(i); 54 : } 55 0 : return r; 56 : } 57 : 58 : /** Returns number of values between {@link #getMin()} and {@link #getMax()} */ 59 : public int getRangeSize() { 60 0 : return max - min; 61 : } 62 : } 63 : 64 : protected String name; 65 : protected int min; 66 : protected int max; 67 : 68 0 : protected PermissionRange() {} 69 : 70 151 : public PermissionRange(String name, int min, int max) { 71 151 : this.name = name; 72 : 73 151 : if (min <= max) { 74 151 : this.min = min; 75 151 : this.max = max; 76 : } else { 77 1 : this.min = 0; 78 1 : this.max = 0; 79 : } 80 151 : } 81 : 82 : public String getName() { 83 0 : return name; 84 : } 85 : 86 : public boolean isLabel() { 87 0 : return Permission.isLabel(getName()); 88 : } 89 : 90 : public String getLabel() { 91 0 : return Permission.extractLabel(getName()); 92 : } 93 : 94 : public int getMin() { 95 103 : return min; 96 : } 97 : 98 : public int getMax() { 99 124 : return max; 100 : } 101 : 102 : /** True if the value is within the range. */ 103 : public boolean contains(int value) { 104 103 : return getMin() <= value && value <= getMax(); 105 : } 106 : 107 : /** Normalize the value to fit within the bounds of the range. */ 108 : public int squash(int value) { 109 0 : return Math.min(Math.max(getMin(), value), getMax()); 110 : } 111 : 112 : /** True both {@link #getMin()} and {@link #getMax()} are 0. */ 113 : public boolean isEmpty() { 114 40 : return getMin() == 0 && getMax() == 0; 115 : } 116 : 117 : @Override 118 : public int compareTo(PermissionRange o) { 119 0 : return getName().compareTo(o.getName()); 120 : } 121 : 122 : @Override 123 : public String toString() { 124 0 : StringBuilder r = new StringBuilder(); 125 0 : if (getMin() < 0 && getMax() == 0) { 126 0 : r.append(getMin()); 127 0 : r.append(' '); 128 : } else { 129 0 : if (getMin() != getMax()) { 130 0 : if (0 <= getMin()) { 131 0 : r.append('+'); 132 : } 133 0 : r.append(getMin()); 134 0 : r.append(".."); 135 : } 136 0 : if (0 <= getMax()) { 137 0 : r.append('+'); 138 : } 139 0 : r.append(getMax()); 140 0 : r.append(' '); 141 : } 142 0 : return r.toString(); 143 : } 144 : }