001 /*
002 * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package de.kumpe.hadooptimizer.impl;
017
018 import org.apache.commons.logging.Log;
019 import org.apache.commons.logging.LogFactory;
020 import org.apache.commons.math.random.RandomGenerator;
021
022 import de.kumpe.hadooptimizer.NeedsRandomGenerator;
023 import de.kumpe.hadooptimizer.Wrapper;
024
025 public abstract class NeedsRandomWrapperBase<T> implements Wrapper<T>,
026 NeedsRandomGenerator {
027 private static final long serialVersionUID = 1L;
028 private static final Log log = LogFactory
029 .getLog(NeedsRandomWrapperBase.class);
030
031 protected final T delegate;
032
033 public NeedsRandomWrapperBase(final T delegate) {
034 if (null == delegate) {
035 throw new NullPointerException("delegate may not be null");
036 }
037
038 this.delegate = delegate;
039 }
040
041 @Override
042 public final T getDelegate() {
043 assert null != delegate;
044
045 return delegate;
046 }
047
048 @Override
049 public void setRandomGenerator(final RandomGenerator randomGenerator) {
050 if (log.isTraceEnabled()) {
051 log.trace("Received new RandomGenerator-instance: " + randomGenerator);
052 }
053
054 if (delegate instanceof NeedsRandomGenerator) {
055 ((NeedsRandomGenerator) delegate).setRandomGenerator(randomGenerator);
056 }
057 }
058
059 @Override
060 public int hashCode() {
061 assert null != delegate;
062
063 final int prime = 31;
064 int result = 1;
065 result = prime * result + delegate.hashCode();
066 return result;
067 }
068
069 @Override
070 public boolean equals(final Object obj) {
071 assert null != delegate;
072
073 if (this == obj) {
074 return true;
075 }
076 if (obj == null) {
077 return false;
078 }
079 if (!(obj instanceof NeedsRandomWrapperBase)) {
080 return false;
081 }
082 final NeedsRandomWrapperBase<?> other = (NeedsRandomWrapperBase<?>) obj;
083 return delegate.equals(other.delegate);
084 }
085
086 @Override
087 public String toString() {
088 return String.format("%s [delegate=%s]", getClass().getSimpleName(),
089 delegate);
090 }
091 }