/** parses a string representation of ports, as "80,8080,8000,8080-8099" */ public static PortRange fromString(String s) { List<PortRange> l = new ArrayList<PortRange>(); for (String si: s.split(",")) { si = si.trim(); int start, end; if (si.endsWith("+")) { String si2 = si.substring(0, si.length()-1).trim(); start = Integer.parseInt(si2); end = MAX_PORT; } else if (si.indexOf('-')>0) { int v = si.indexOf('-'); start = Integer.parseInt(si.substring(0, v).trim()); end = Integer.parseInt(si.substring(v+1).trim()); } else if (si.length()==0) { //nothing, ie empty range, just continue continue; } else { //should be number on its own l.add(new SinglePort(Integer.parseInt(si))); continue; } l.add(new LinearPortRange(start, end)); } if (l.size() == 1) { return l.get(0); } else { return new AggregatePortRange(l); } }
public static PortRange fromIterable(Iterable<?> c) { List<PortRange> l = new ArrayList<PortRange>(); for (Object o: c) { if (o instanceof Integer) l.add(fromInteger((Integer)o)); else if (o instanceof String) for (String string : JavaStringEscapes.unwrapJsonishListIfPossible((String)o)) l.add(fromString(string)); else if (o instanceof Iterable) l.add(fromIterable((Iterable<?>)o)); else if (o instanceof int[]) l.add(fromIterable(Ints.asList((int[])o))); else if (o instanceof String[]) for (String string : (String[])o) l.add(fromString(string)); else if (o instanceof Object[]) for (Object object : (Object[])o) if (object instanceof Integer) l.add(fromInteger((Integer)object)); else if (object instanceof String) l.add(fromString((String)object)); else throw new IllegalArgumentException("'" + object + "' must be of type Integer or String"); else l.add(TypeCoercions.coerce(o, PortRange.class)); } return new AggregatePortRange(l); }