/* * 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.ConfigException; import org.wamblee.xmlrouter.config.Identifiable; /** * Robust identifiable provides robustness for identifiable objects. * * @author Erik Brakkee * * @param */ public class RobustIdentifiable implements Identifiable { private static final Logger LOGGER = Logger .getLogger(RobustIdentifiable.class.getName()); private Id id; // TODO test this class. // TODO test that id is constant even though delegated changes its id. public RobustIdentifiable(String aPrefix, Identifiable aIdentifiable) { notNull("prefix", aPrefix); notNull("identifiable", aIdentifiable); // TODO test id is null // TODO getId() throws exception try { id = aIdentifiable.getId(); if (id == null) { throwConfigException("identifiable.getId() returned null", null); } id = new Id(aPrefix + id.getId()); } 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("id is null"); } @Override public Id getId() { return id; } // TODO test equals, hashcode. @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()); } }