FuzzyWaypointCache.java 2.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.util.geo.maps.osm;

import java.util.Map;

import com.google.common.collect.Maps;

27
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import de.tud.kom.p2psim.impl.topology.waypoints.graph.Waypoint;

class FuzzyWaypointCache {
	private Map<FuzzyWaypointCache.PositionWrapper, Waypoint> waypointCache = Maps
			.newHashMap();

	public FuzzyWaypointCache() {
		//
	}

	public void addWaypoint(PositionVector position, Waypoint waypoint) {
		if (getWaypoint(position) == null) {
			waypointCache.put(new PositionWrapper(position), waypoint);
		}
	}

	public Waypoint getWaypoint(PositionVector position) {
		return waypointCache.get(new PositionWrapper(position));
	}

	public void clear() {
		waypointCache.clear();
	}

	private static class PositionWrapper {
		private PositionVector position;

		public PositionWrapper(PositionVector position) {
			this.position = position;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime
					* result
					+ ((position == null) ? 0 : addTolerance(position)
							.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (!(obj instanceof FuzzyWaypointCache.PositionWrapper))
				return false;

			PositionVector pw1 = addTolerance(position);
			PositionVector pw2 = addTolerance(((FuzzyWaypointCache.PositionWrapper) obj).position);

			if (!pw1.equals(pw2))
				return false;

			return true;
		}

		private PositionVector addTolerance(PositionVector position) {
			int count = 1;
			PositionVector tpos = new PositionVector(round(position.getX(),
					count), round(position.getY(), count));

			return tpos;
		}

		public static final double round(double value, int count) {
			double shift = Math.pow(10, count);
			return Math.round(value * shift) / shift;
		}
	}
}