Created
January 9, 2012 18:44
-
-
Save redrick/1584289 to your computer and use it in GitHub Desktop.
addinional java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
================ some work with containers on specific container.... | |
public class CountryUnionImpl implements CountryUnion { | |
private Set<Country> countries = new HashSet<Country>(); | |
public boolean add(Country country) { | |
if (country == null) return false; | |
if (countries.contains(country)) return false; | |
return countries.add(country); | |
} | |
public boolean remove(Country country) { | |
if (country == null) return false; | |
return countries.remove(country); | |
} | |
public int getPopulation() { | |
int result = 0; | |
for (Iterator it = countries.iterator(); it.hasNext(); ) { | |
Country c = (Country) it.next(); | |
result += c.getPopulation(); | |
} | |
return result; | |
} | |
public Collection<Country> getCountries() { | |
return Collections.unmodifiableSet(countries); | |
} | |
public String toString() { | |
return countries.toString(); | |
} | |
} | |
====================================== more helpfull maybe from here | |
public class MapPolygon implements GraphicObject | |
{ | |
private SortedMap<String, Point> vertices = new TreeMap<String,Point>(); | |
public boolean addVertex(String name, Point p) { | |
if (name == null || p == null) return false; | |
if (name.length() != 1) return false; | |
if (vertices.containsKey(name)) return false; | |
vertices.put(name, p); | |
return true; | |
} | |
public Point getVertex(String name) { | |
return vertices.get(name); | |
} | |
public Set<String> getVertex(Point p) { | |
Set<String> ret = new HashSet<String>(); | |
for (String name : vertices.keySet()) { | |
if (vertices.get(name).equals(p)) ret.add(name); | |
} | |
return ret; | |
} | |
@Override | |
public String toString() { | |
return vertices.toString(); | |
} | |
public double getPerimeter() { | |
double ret = 0.0; | |
Point prev = null; | |
for (Point act : vertices.values()) { | |
if (prev == null) { | |
prev = act; | |
continue; | |
} | |
ret += prev.dist(act); | |
prev = act; | |
} | |
ret += prev.dist(vertices.get(vertices.firstKey())); | |
return ret; | |
} | |
public SortedSet<Point> getSortedPoints() { | |
return new TreeSet<Point>(vertices.values()); | |
} | |
public SortedSet<Point> getSortedPoints(Comparator<Point> comp) { | |
SortedSet<Point> ret = new TreeSet<Point>(comp); | |
ret.addAll(vertices.values()); | |
return ret; | |
} | |
public boolean equals(Object obj) { | |
if (!(obj instanceof MapPolygon)) return false; | |
return ((MapPolygon)obj).vertices.equals(this.vertices); | |
} | |
public int hashCode() { | |
return vertices.hashCode(); | |
} | |
========= in here there is a little somtehnig with input/output | |
public void write(OutputStream out) throws IOException { | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); | |
for (String name : vertices.keySet()) { | |
writer.write(vertices.get(name).getX()); | |
writer.write(" "); | |
writer.write(vertices.get(name).getY()); | |
writer.write(""); | |
writer.write(name); | |
writer.newLine(); | |
} | |
writer.flush(); | |
} | |
public void write(String file) throws IOException { | |
write(new File(file)); | |
} | |
public void write(File file) throws IOException { | |
FileOutputStream out = new FileOutputStream(file); | |
try { | |
write(out); | |
} | |
finally { | |
out.close(); | |
} | |
} | |
public void write() throws IOException { | |
write(System.out); | |
} | |
public void read(File file) throws IOException { | |
BufferedReader reader = new BufferedReader(new FileReader(file)); | |
String line = reader.readLine(); | |
while (line != null) { | |
String[] ss = line.split("", 3); // pozri si StringBuilder | |
if (ss.length != 3) throw new IOException("Wrong data format"); | |
try { | |
int x = Integer.parseInt(ss[0]); | |
int y = Integer.parseInt(ss[1]); | |
vertices.put(ss[2], new Point(x,y)); | |
} catch(NumberFormatException ex) { | |
throw new IOException("wrong format of data", ex); | |
} | |
line = reader.readLine(); | |
} | |
} | |
} | |
================================= aaaaaaaaaaaaaaaand this one here with list | |
public class ListPolygon implements GraphicObject | |
{ | |
private List<Point> vertices; | |
public ListPolygon(List<Point> vertices) { | |
if (vertices == null) throw new NullPointerException("vertices"); | |
if (vertices.contains(null)) throw new NullPointerException("vertices"); | |
this.vertices = new ArrayList<Point>(vertices); | |
//this.vertices.addAll(vertices); | |
} | |
/** | |
* @return false if p is null, true otherwise | |
*/ | |
public boolean addPoint(Point p) { | |
if (p == null) return false; | |
return vertices.add(p); | |
} | |
/** | |
* @return number of really removed vertices | |
*/ | |
public int removePoints(int from, int to) { | |
int ff = from; | |
int tt = to; | |
if (from > to) { | |
ff = to; | |
tt = from; | |
} | |
if (ff < 0) ff = 0; | |
if (tt >= vertices.size()) tt = vertices.size() - 1; | |
List<Point> aux = new ArrayList<Point>(); | |
for (int i = ff; i <= tt; i++) { | |
aux.add(vertices.get(i)); | |
} | |
vertices.removeAll(aux); | |
return tt-ff+1; | |
} | |
public List<Point> getVertices() { | |
return Collections.unmodifiableList(vertices); | |
//return new ArraList<Point>(vertices); | |
} | |
public ListPolygon cut(int maxX) { | |
List<Point> aux = new ArrayList<Point>(); | |
for (Point p: vertices) { | |
if (p.getX() <= maxX) aux.add(p); | |
} | |
return new ListPolygon(aux); | |
} | |
public void cutMe1(int maxX) { | |
for (int i = 0; i < vertices.size(); i++) { | |
if (vertices.get(i).getX() > maxX) { | |
vertices.remove(i); | |
i--; | |
} | |
} | |
} | |
public void cutMe2(int maxX) { | |
List<Point> aux = new ArrayList<Point>(); | |
for (Point p: vertices) { | |
if (p.getX() <= maxX) { | |
aux.add(p); | |
} | |
} | |
vertices = aux; | |
//vertices.removeAll(aux); | |
} | |
public void cutMe3(int maxX) { | |
for (Iterator<Point> it = vertices.iterator(); it.hasNext(); ) { | |
Point p = it.next(); | |
if (p.getX() > maxX) it.remove(); | |
} | |
} | |
public boolean equals(Object obj) { | |
if (!(obj instanceof ListPolygon)) return false; | |
//ListPolygon p = (ListPolygon) obj; | |
return ((ListPolygon)obj).vertices.equals(this.vertices); | |
} | |
public int hashCode() { | |
return vertices.hashCode(); | |
} | |
public double getPerimeter() { | |
double ret = 0.0; | |
for (int i = 0; i < vertices.size(); i++) { | |
ret += vertices.get(i).dist(vertices.get((i+1)%vertices.size())); | |
} | |
return ret; | |
} | |
} | |
===================== belongs to the first one with the Map === > | |
public class Point implements Comparable<Point> { | |
private int x; | |
private int y; | |
public Point(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
public String toString() { | |
return "[" + x + ", " + y + "]"; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y; | |
} | |
public double dist(Point p) { | |
if (p == null) return -1.0; | |
int dx = this.getX() - p.getX(); | |
int dy = this.getY() - p.getY(); | |
return Math.sqrt(dx * dx + dy * dy); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (!(obj instanceof Point)) return false; | |
Point p = (Point) obj; | |
return p.x == x && p.y == y; | |
} | |
@Override | |
public int hashCode() { | |
return x|y; | |
} | |
@Override // to let the compilator know I wanted override somthing if there is a typo | |
public int compareTo(Point obj) { | |
int diff = this.x - obj.x; | |
if (diff != 0) return diff; | |
return this.y - obj.y; | |
} | |
} | |
================== with the comppareTo overriden there comparator comes to my mind | |
public class XOnlyComparator implements Comparator<Point> | |
{ | |
public int compare(Point obj1, Point obj2) { | |
return obj1.getX() - obj2.getX(); | |
} | |
} | |
================= I played a little with input/output in this one here | |
public class CountriesIOImpl extends CountriesImpl implements CountriesIO { | |
private Set<Country> countries = new HashSet<Country>(); | |
public boolean load(InputStream input) throws CountriesException { | |
BufferedReader loader = new BufferedReader(new InputStreamReader(input)); | |
try { | |
String line = loader.readLine(); | |
while (line != null) { | |
String[] ss = line.split(" ", 2); | |
if (ss.length != 2) throw new IOException("Wrong data format"); | |
try { | |
int population = Integer.parseInt(ss[0]); | |
String name = new String(ss[1]); | |
//System.out.println(countries); | |
countries.add(new Country(name, population)); | |
} catch(NumberFormatException ex) { | |
throw new IOException("wrong format of data", ex); | |
} | |
line = loader.readLine(); | |
} | |
} catch (IOException ex) { | |
throw new CountriesException("read error", ex); | |
} | |
return true; | |
} | |
public void save(OutputStream output) throws CountriesException { | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output)); | |
try { | |
for (Iterator it = countries.iterator(); it.hasNext(); ) { | |
Country c = (Country) it.next(); | |
writer.write(c.getPopulation() + " " + c.getName()); | |
writer.newLine(); | |
} | |
writer.flush(); | |
} catch (IOException ex) { | |
throw new CountriesException("write error", ex); | |
} | |
} | |
} |
with the custom comparator you do this kind of stuff
public SortedSet<Point> getSortedPoints(Comparator<Point> comp) {
SortedSet<Point> ret = new TreeSet<Point>(comp);
ret.addAll(vertices.values());
return ret;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
some other input/output usage