yacy_search_server/source/net/yacy/document/geolocalization/OverarchingLocalization.java

166 lines
5.2 KiB
Java
Raw Normal View History

/**
* OverarchingLocalization.java
* Copyright 2010 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
* first published 16.05.2010 on http://yacy.net
*
* This file is part of YaCy Content Integration
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/
package net.yacy.document.geolocalization;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
public class OverarchingLocalization implements Localization {
private final Map<String, Localization> services;
/**
* create a new overarching localization object
*/
public OverarchingLocalization() {
this.services = new HashMap<String, Localization>();
}
/**
* add a localization service
* @param nickname the nickname of the service
* @param service the service
*/
public void addLocalization(final String nickname, final Localization service) {
this.services.put(nickname, service);
}
/**
* remove a localization service
* @param nickname
*/
public void removeLocalization(final String nickname) {
this.services.remove(nickname);
}
/**
* the number of locations that this localization stores
* @return the number of locations
*/
@Override
public int locations() {
int locations = 0;
for (final Localization service: this.services.values()) {
locations += service.locations();
}
return locations;
}
/**
* find a location by name
* @param anyname - a name of a location
* @param locationexact - if true, then only exact matched with the location are returned. if false also partially matching names
* @return a set of locations, ordered by population (if this information is given)
*/
@Override
public TreeSet<Location> find(final String anyname, final boolean locationexact) {
final TreeSet<Location> locations = new TreeSet<Location>();
for (final Localization service: this.services.values()) {
locations.addAll(service.find(anyname, locationexact));
}
return locations;
}
/**
* produce a set of location names
* @return a set of names
*/
@Override
public Set<String> locationNames() {
final Set<String> locations = new HashSet<String>();
for (final Localization service: this.services.values()) {
locations.addAll(service.locationNames());
}
return locations;
}
/**
* recommend a set of names according to a given name
* @param s a possibly partially matching name
* @return a set of names that match with the given name using the local dictionary of names
*/
@Override
public Set<String> recommend(final String s) {
final Set<String> recommendations = new HashSet<String>();
if (s.length() == 0) {
return recommendations;
}
for (final Localization service: this.services.values()) {
recommendations.addAll(service.recommend(s));
}
return recommendations;
}
/**
* recommend a set of names according to a given name
* @param s a possibly partially matching name
* @return a set of names that match with the given name using the local dictionary of names
*/
@Override
public Set<StringBuilder> recommend(final StringBuilder s) {
final Set<StringBuilder> recommendations = new HashSet<StringBuilder>();
if (s.length() == 0) {
return recommendations;
}
for (final Localization service: this.services.values()) {
recommendations.addAll(service.recommend(s));
}
return recommendations;
}
/**
* return an nickname of the localization service
* @return the nickname
*/
@Override
public String nickname() {
return "oa";
}
/**
* hashCode that must be used to distinguish localization services in hash sets
* @return the hash code, may be derived from the nickname
*/
@Override
public int hashCode() {
return nickname().hashCode();
}
/**
* compare localization services; to be used for hash sets with localization services
* @param other
* @return true if both objects are localization services and have the same nickname
*/
@Override
public boolean equals(final Object other) {
if (!(other instanceof Localization)) {
return false;
}
return nickname().equals(((Localization) other).nickname());
}
}