/*
 *
 *  Copyright 2010-2014 Orient Technologies LTD (info(at)orientechnologies.com)
 *  
 *  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 com.orientechnologies.common.factory;

import com.orientechnologies.common.exception.OException;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Configurable stateful factory. New instances are created when newInstance() is called, invoking its default empty constructor.
 * 
 * @param <K>
 *          Factory key
 * @param <V>
 *          Instance type
 */
public class OConfigurableStatefulFactory<K, V> {
  protected final Map<K, Class<? extends V>> registry = new LinkedHashMap<K, Class<? extends V>>();
  protected Class<? extends V>               defaultClass;

  public Class<? extends V> get(final K iKey) {
    return registry.get(iKey);
  }

  public V newInstance(final K iKey) {
    if (iKey == null && defaultClass == null)
      throw new IllegalArgumentException("Cannot create implementation for type null");

    final Class<? extends V> cls = registry.get(iKey);
    if (cls != null) {
      try {
        return cls.newInstance();
      } catch (Exception e) {
        throw new OException(String.format("Error on creating new instance of class '%s' registered in factory with key '%s'", cls,
            iKey), e);
      }
    }

    return newInstanceOfDefaultClass();
  }

  public V newInstanceOfDefaultClass() {
    if (defaultClass != null) {
      try {
        return defaultClass.newInstance();
      } catch (Exception e) {
        throw new OException(String.format("Error on creating new instance of default class '%s'", defaultClass), e);
      }
    }
    return null;
  }

  public OConfigurableStatefulFactory<K, V> register(final K iKey, final Class<? extends V> iValue) {
    registry.put(iKey, iValue);
    return this;
  }

  public OConfigurableStatefulFactory<K, V> unregister(final K iKey) {
    registry.remove(iKey);
    return this;
  }

  public OConfigurableStatefulFactory<K, V> unregisterAll() {
    registry.clear();
    return this;
  }

  public Class<? extends V> getDefaultClass() {
    return defaultClass;
  }

  public <C extends Class<? extends V>> OConfigurableStatefulFactory<K, V> setDefaultClass(final C defaultClass) {
    this.defaultClass = defaultClass;
    return this;
  }
}

@@ -821,16 +821,38 @@
 til.Map;
+%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -2208,24 +2208,90 @@
 rn null;%0A  %7D
+%0A  %0A  public Set%3CK%3E getKeys()%0A  %7B%0A%09  return registry.keySet();%0A  %7D
 %0A%0A  public O

Add ability to obtain all possible registry keys