d5f4b619f39ca8f429c785e8188ada8be2c8588a
2 * Copyright (C) 2012-2017 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 package org
.strongswan
.android
.utils
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Collection
;
20 import java
.util
.Iterator
;
21 import java
.util
.List
;
22 import java
.util
.TreeSet
;
25 * Class that represents a set of IP address ranges (not necessarily proper subnets) and allows
26 * modifying the set and enumerating the resulting subnets.
28 public class IPRangeSet
implements Iterable
<IPRange
>
30 private TreeSet
<IPRange
> mRanges
= new TreeSet
<>();
33 * Parse the given string (space separated ranges in CIDR or range notation) and return the
34 * resulting set or {@code null} if the string was invalid. An empty set is returned if the given string
37 public static IPRangeSet
fromString(String ranges
)
39 IPRangeSet set
= new IPRangeSet();
42 for (String range
: ranges
.split("\\s+"))
46 set
.add(new IPRange(range
));
48 catch (Exception unused
)
49 { /* besides due to invalid strings exceptions might get thrown if the string
50 * contains a hostname (NetworkOnMainThreadException) */
59 * Add a range to this set. Automatically gets merged with existing ranges.
61 public void add(IPRange range
)
63 if (mRanges
.contains(range
))
70 Iterator
<IPRange
> iterator
= mRanges
.iterator();
71 while (iterator
.hasNext())
73 IPRange existing
= iterator
.next();
74 IPRange replacement
= existing
.merge(range
);
75 if (replacement
!= null
)
88 * Add all ranges from the given set.
90 public void add(IPRangeSet ranges
)
96 for (IPRange range
: ranges
.mRanges
)
103 * Add all ranges from the given collection to this set.
105 public void addAll(Collection
<?
extends IPRange
> coll
)
107 for (IPRange range
: coll
)
114 * Remove the given range from this set. Existing ranges are automatically adjusted.
116 public void remove(IPRange range
)
118 ArrayList
<IPRange
> additions
= new ArrayList
<>();
119 Iterator
<IPRange
> iterator
= mRanges
.iterator();
120 while (iterator
.hasNext())
122 IPRange existing
= iterator
.next();
123 List
<IPRange
> result
= existing
.remove(range
);
124 if (result
.size() == 0)
128 else if (!result
.get(0).equals(existing
))
131 additions
.addAll(result
);
134 mRanges
.addAll(additions
);
138 * Remove the given ranges from ranges in this set.
140 public void remove(IPRangeSet ranges
)
147 for (IPRange range
: ranges
.mRanges
)
154 * Get all the subnets derived from all the ranges in this set.
156 public Iterable
<IPRange
> subnets()
158 return new Iterable
<IPRange
>()
161 public Iterator
<IPRange
> iterator()
163 return new Iterator
<IPRange
>()
165 private Iterator
<IPRange
> mIterator
= mRanges
.iterator();
166 private List
<IPRange
> mSubnets
;
169 public boolean hasNext()
171 return (mSubnets
!= null
&& mSubnets
.size() > 0) || mIterator
.hasNext();
175 public IPRange
next()
177 if (mSubnets
== null
|| mSubnets
.size() == 0)
179 IPRange range
= mIterator
.next();
180 mSubnets
= range
.toSubnets();
182 return mSubnets
.remove(0);
188 throw new UnsupportedOperationException();
196 public Iterator
<IPRange
> iterator()
198 return mRanges
.iterator();
202 * Returns the number of ranges, not subnets.
206 return mRanges
.size();