Java tutorial
/* * Copyright 2013 Jeremy Gustie * * 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.codeseed.common.config; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Map; import java.util.Objects; import javax.annotation.Nullable; import com.google.common.base.Converter; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableMap; import com.google.common.reflect.AbstractInvocationHandler; /** * Handles the invocation of configuration methods. * * @author Jeremy Gustie */ class ConfigurationHandler extends AbstractInvocationHandler { /** * The source of property information. */ private final PropertySource source; /** * Mapping of return types to the converter that should handle it. */ private final Map<Type, Converter<String, ?>> converters; ConfigurationHandler(PropertySource source, Map<Type, Converter<String, ?>> converters) { this.source = checkNotNull(source); this.converters = ImmutableMap.copyOf(converters); } @Override protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable { final Class<?> declaringClass = method.getDeclaringClass(); checkState(Configuration.class.isAssignableFrom(declaringClass), "expecting Configuration interface"); checkState(declaringClass.isInterface(), "expecting interface"); checkState(args.length == 0, "expecting zero argument method"); return convert(method.getGenericReturnType(), getProperty(method)); } protected String getProperty(Method method) { final String value = source.getProperty(method); if (value != null) { return value; } else { throw new MissingPropertyException(method); } } protected Object convert(Type type, String value) { final Converter<String, ?> converter = converters.get(type); if (converter != null) { return converter.convert(value); } else { throw new PropertyConversionException(type); } } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof ConfigurationHandler) { ConfigurationHandler other = (ConfigurationHandler) obj; return Objects.equals(source, other.source) && Objects.equals(converters, other.converters); } return false; } @Override public int hashCode() { return Objects.hash(source, converters); } @Override public String toString() { return MoreObjects.toStringHelper(getClass()).add("source", source).add("converters", converters) .toString(); } }