package org.apache.helix.customizedstate;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.util.HashMap;
import org.apache.helix.HelixException;
import org.apache.helix.HelixManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Singleton factory that build customized state provider.
 */
public class CustomizedStateProviderFactory {
  private static Logger LOG = LoggerFactory.getLogger(CustomizedStateProvider.class);
  private final HashMap<String, CustomizedStateProvider> _customizedStateProviderMap =
      new HashMap<>();
  private HelixManager _helixManager;

  protected CustomizedStateProviderFactory() {
  }

  private static class SingletonHelper {
    private static final CustomizedStateProviderFactory INSTANCE =
        new CustomizedStateProviderFactory();
  }

  public static CustomizedStateProviderFactory getInstance() {
    return SingletonHelper.INSTANCE;
  }

  public CustomizedStateProvider buildCustomizedStateProvider(String instanceName) {
    if (_helixManager == null) {
      throw new HelixException("Helix Manager has not been set yet.");
    }
    return buildCustomizedStateProvider(_helixManager, instanceName);
  }

  /**
   * Build a customized state provider based on the specified input. If the instance already has a
   * provider, return it. Otherwise, build a new one and put it in the map.
   * @param helixManager The helix manager that belongs to the instance
   * @param instanceName The name of the instance
   * @return CustomizedStateProvider
   */
  public CustomizedStateProvider buildCustomizedStateProvider(HelixManager helixManager,
      String instanceName) {
    synchronized (_customizedStateProviderMap) {
      if (_customizedStateProviderMap.get(instanceName) != null) {
        return _customizedStateProviderMap.get(instanceName);
      }
      CustomizedStateProvider customizedStateProvider =
          new CustomizedStateProvider(helixManager, instanceName);
      _customizedStateProviderMap.put(instanceName, customizedStateProvider);
      return customizedStateProvider;
    }
  }

  public void setHelixManager(HelixManager helixManager) {
    _helixManager = helixManager;
  }
}

@@ -858,25 +858,37 @@
 ort 
-java.util.HashMap
+org.apache.helix.HelixManager
 ;%0Aim
@@ -914,25 +914,30 @@
 ix.Helix
-Exception
+ManagerFactory
 ;%0Aimport
@@ -950,36 +950,36 @@
 pache.helix.
-HelixManager
+InstanceType
 ;%0Aimport org
@@ -1228,156 +1228,8 @@
 ss);
-%0A  private final HashMap%3CString, CustomizedStateProvider%3E _customizedStateProviderMap =%0A      new HashMap%3C%3E();%0A  private HelixManager _helixManager;
 %0A%0A  
@@ -1548,860 +1548,569 @@
 %0A%0A  
-public CustomizedStateProvider buildCustomizedStateProvider(String instanceName) %7B%0A    if (_helixManager == null) %7B%0A      throw new HelixException(%22Helix Manager has not been set yet.%22);%0A    %7D%0A    return buildCustomizedStateProvider(_helixManager, instanceName);%0A  %7D%0A%0A  /**%0A   * Build a customized state provider based on the specified input. If the instance already has a%0A   * provider, return it. Otherwise, build a new one and put it in the map.%0A   * @param helixManager The helix manager that belongs to the instance%0A   * @param instanceName The name of the instance%0A   * @return CustomizedStateProvider%0A   */%0A  public CustomizedStateProvider buildCustomizedStateProvider(HelixManager helixManager,%0A      String instanceName) %7B%0A    synchronized (_customizedStateProviderMap) %7B%0A      if (_customizedStateProviderMap.get(instanceName) != null) %7B%0A    
+/**%0A   * Build a customized state provider based on user input.%0A   * @param instanceName The name of the instance%0A   * @param clusterName The name of the cluster that has the instance%0A   * @param zkAddress The zookeeper address of the cluster%0A   * @return CustomizedStateProvider%0A   */%0A  public CustomizedStateProvider buildCustomizedStateProvider(String instanceName,%0A      String clusterName, String zkAddress) %7B%0A    HelixManager helixManager = HelixManagerFactory%0A        .getZKHelixManager(clusterName, instanceName, InstanceType.ADMINISTRATOR, zkAddress);%0A
     
@@ -2116,18 +2116,21 @@
  return 
-_c
+new C
 ustomize
@@ -2147,16 +2147,23 @@
 ider
-Map.get(
+(helixManager, 
 inst
@@ -2179,21 +2179,31 @@
 ;%0A  
-    %7D%0A      C
+%7D%0A%0A  /**%0A   * Build a c
 usto
@@ -2207,30 +2207,32 @@
 stomized
-S
+ s
 tate
-P
+ p
 rovider 
 customiz
@@ -2227,154 +2227,188 @@
 der 
-customizedStateProvider =%0A          new CustomizedStateProvider(helixManager, instanceName);%0A      _customizedStateProviderMap.put(instanceName, c
+based on user input.%0A   * @param instanceName The name of the instance%0A   * @param helixManager Helix manager provided by user%0A   * @return CustomizedStateProvider%0A   */%0A  public C
 usto
@@ -2429,25 +2429,15 @@
 ider
-);%0A      return c
+ buildC
 usto
@@ -2458,38 +2458,9 @@
 ider
-;%0A    %7D%0A  %7D%0A%0A  public void set
+(
 Heli
@@ -2467,18 +2467,18 @@
 xManager
-(H
+ h
 elixMana
@@ -2484,57 +2484,105 @@
 ager
- helixManager) %7B%0A    _helixManager = helixManager
+,%0A      String instanceName) %7B%0A    return new CustomizedStateProvider(helixManager, instanceName)
 ;%0A  

fix customized state provider (#928)