X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=router%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FRobustIdentifiable.java;fp=router%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FRobustIdentifiable.java;h=f371975fac804c378162084bc0e1a74536fbe2d9;hb=8e41cb29f75362a792292d21b481bd06a9506296;hp=0000000000000000000000000000000000000000;hpb=9dbc2844950b55ae552fe74840954ea71b754c7a;p=xmlrouter diff --git a/router/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java b/router/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java new file mode 100644 index 0000000..f371975 --- /dev/null +++ b/router/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java @@ -0,0 +1,79 @@ +/* + * Copyright 2005-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.xmlrouter.impl; + +import static org.wamblee.xmlrouter.impl.MessageUtil.*; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.config.Identifiable; + +/** + * Robust identifiable provides robustness for identifiable objects. It adds + * equality based on the id. + * + * @author Erik Brakkee + * + * @param + */ +public class RobustIdentifiable implements Identifiable { + private static final Logger LOGGER = Logger + .getLogger(RobustIdentifiable.class.getName()); + + private Id id; + + public RobustIdentifiable(Identifiable aIdentifiable) { + notNull("identifiable", aIdentifiable); + try { + id = aIdentifiable.getId(); + if (id == null) { + throwConfigException("identifiable.getId() returned null", null); + } + } catch (Exception e) { + throwConfigException("identifiable.getId() threw exception", e); + } + + } + + private void throwConfigException(String aMsg, Exception aException) { + LOGGER.log(Level.WARNING, aMsg, aException); + throw new ConfigException(aMsg); + } + + @Override + public Id getId() { + return id; + } + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object aObj) { + if (aObj == null) { + return false; + } + if (!getClass().isInstance(aObj)) { + return false; + } + RobustIdentifiable obj = (RobustIdentifiable) aObj; + return id.equals(obj.getId()); + } +}