• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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.android.apksig.internal.util;
18 
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 
23 /**
24  * Inclusive interval of integers.
25  */
26 public class InclusiveIntRange {
27     private final int min;
28     private final int max;
29 
InclusiveIntRange(int min, int max)30     private InclusiveIntRange(int min, int max) {
31         this.min = min;
32         this.max = max;
33     }
34 
getMin()35     public int getMin() {
36         return min;
37     }
38 
getMax()39     public int getMax() {
40         return max;
41     }
42 
fromTo(int min, int max)43     public static InclusiveIntRange fromTo(int min, int max) {
44         return new InclusiveIntRange(min, max);
45     }
46 
from(int min)47     public static InclusiveIntRange from(int min) {
48         return new InclusiveIntRange(min, Integer.MAX_VALUE);
49     }
50 
getValuesNotIn( List<InclusiveIntRange> sortedNonOverlappingRanges)51     public List<InclusiveIntRange> getValuesNotIn(
52             List<InclusiveIntRange> sortedNonOverlappingRanges) {
53         if (sortedNonOverlappingRanges.isEmpty()) {
54             return Collections.singletonList(this);
55         }
56 
57         int testValue = min;
58         List<InclusiveIntRange> result = null;
59         for (InclusiveIntRange range : sortedNonOverlappingRanges) {
60             int rangeMax = range.max;
61             if (testValue > rangeMax) {
62                 continue;
63             }
64             int rangeMin = range.min;
65             if (testValue < range.min) {
66                 if (result == null) {
67                     result = new ArrayList<>();
68                 }
69                 result.add(fromTo(testValue, rangeMin - 1));
70             }
71             if (rangeMax >= max) {
72                 return (result != null) ? result : Collections.emptyList();
73             }
74             testValue = rangeMax + 1;
75         }
76         if (testValue <= max) {
77             if (result == null) {
78                 result = new ArrayList<>(1);
79             }
80             result.add(fromTo(testValue, max));
81         }
82         return (result != null) ? result : Collections.emptyList();
83     }
84 
85     @Override
toString()86     public String toString() {
87         return "[" + min + ", " + ((max < Integer.MAX_VALUE) ? (max + "]") : "\u221e)");
88     }
89 }
90