001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang.implementation; 020 021import org.apache.reef.tang.ClassHierarchy; 022import org.apache.reef.tang.Configuration; 023import org.apache.reef.tang.ConfigurationBuilder; 024import org.apache.reef.tang.ExternalConstructor; 025import org.apache.reef.tang.types.ClassNode; 026import org.apache.reef.tang.types.ConstructorDef; 027import org.apache.reef.tang.types.NamedParameterNode; 028 029import java.util.Iterator; 030import java.util.List; 031import java.util.Map.Entry; 032import java.util.Set; 033 034public class ConfigurationImpl implements Configuration { 035 final ConfigurationBuilderImpl builder; 036 037 protected ConfigurationImpl(final ConfigurationBuilderImpl builder) { 038 this.builder = builder; 039 } 040 041 @Override 042 public String getNamedParameter(final NamedParameterNode<?> np) { 043 return builder.namedParameters.get(np); 044 } 045 046 @Override 047 @SuppressWarnings("unchecked") 048 public <T> ClassNode<ExternalConstructor<T>> getBoundConstructor( 049 final ClassNode<T> cn) { 050 return (ClassNode<ExternalConstructor<T>>) builder.boundConstructors.get(cn); 051 } 052 053 @Override 054 public Set<ClassNode<?>> getBoundImplementations() { 055 return builder.boundImpls.keySet(); 056 } 057 058 @Override 059 public Set<ClassNode<?>> getBoundConstructors() { 060 return builder.boundConstructors.keySet(); 061 } 062 063 @Override 064 public Set<NamedParameterNode<?>> getNamedParameters() { 065 return builder.namedParameters.keySet(); 066 } 067 068 @Override 069 public Set<ClassNode<?>> getLegacyConstructors() { 070 return builder.legacyConstructors.keySet(); 071 } 072 073 @Override 074 @SuppressWarnings("unchecked") 075 public <T> ClassNode<T> getBoundImplementation(final ClassNode<T> cn) { 076 return (ClassNode<T>) builder.boundImpls.get(cn); 077 } 078 079 @Override 080 @SuppressWarnings("unchecked") 081 public <T> ConstructorDef<T> getLegacyConstructor(final ClassNode<T> cn) { 082 return (ConstructorDef<T>) builder.legacyConstructors.get(cn); 083 } 084 085 @Override 086 public ConfigurationBuilder newBuilder() { 087 return builder.build().builder; 088 } 089 090 @Override 091 public ClassHierarchy getClassHierarchy() { 092 return builder.namespace; 093 } 094 095 @Override 096 public Set<Object> getBoundSet(NamedParameterNode<Set<?>> np) { 097 return this.builder.boundSetEntries.getValuesForKey(np); 098 } 099 100 @Override 101 public List<Object> getBoundList(NamedParameterNode<List<?>> np) { 102 return this.builder.boundLists.get(np); 103 } 104 105 @Override 106 public Iterable<Entry<NamedParameterNode<Set<?>>, Object>> getBoundSets() { 107 return new Iterable<Entry<NamedParameterNode<Set<?>>, Object>>() { 108 109 @Override 110 public Iterator<Entry<NamedParameterNode<Set<?>>, Object>> iterator() { 111 return builder.boundSetEntries.iterator(); 112 } 113 }; 114 } 115 116 @Override 117 public Set<NamedParameterNode<List<?>>> getBoundLists() { 118 return builder.boundLists.keySet(); 119 } 120 121 @Override 122 public boolean equals(Object o) { 123 if (this == o) { 124 return true; 125 } 126 if (o == null || getClass() != o.getClass()) { 127 return false; 128 } 129 130 final ConfigurationImpl that = (ConfigurationImpl) o; 131 132 if (builder != null ? !builder.equals(that.builder) : that.builder != null) { 133 return false; 134 } 135 136 return true; 137 } 138 139 @Override 140 public int hashCode() { 141 return builder != null ? builder.hashCode() : 0; 142 } 143}