/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * 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.intellij.vcs.log.data;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

@State(name = "Vcs.Log.Tabs.Properties", storages = {@Storage(file = StoragePathMacros.WORKSPACE_FILE)})
public class VcsLogTabsProperties implements PersistentStateComponent<VcsLogTabsProperties.State> {
  public static final String MAIN_LOG_ID = "MAIN";
  private final VcsLogUiPropertiesDeprecated myDeprecatedProperties;
  private State myState = new State();

  public VcsLogTabsProperties(@NotNull VcsLogUiProperties deprecatedProperties) {
    myDeprecatedProperties = (VcsLogUiPropertiesDeprecated)deprecatedProperties;
  }

  @Nullable
  @Override
  public State getState() {
    return myState;
  }

  @Override
  public void loadState(State state) {
    myState = state;
    if (myState.TAB_STATES.get(MAIN_LOG_ID) == null) {
      myState.TAB_STATES.put(MAIN_LOG_ID, myDeprecatedProperties.getState());
    }
  }

  public VcsLogUiProperties createProperties(@NotNull final String id) {
    if (myState.TAB_STATES.get(id) == null) {
      myState.TAB_STATES.put(id, new VcsLogUiPropertiesImpl.State());
    }
    return new VcsLogUiPropertiesImpl() {
      @NotNull
      @Override
      public State getState() {
        return myState.TAB_STATES.get(id);
      }

      @Override
      public void loadState(State state) {
        myState.TAB_STATES.put(id, state);
      }
    };
  }

  public static class State {
    public Map<String, VcsLogUiPropertiesImpl.State> TAB_STATES = ContainerUtil.newTreeMap();
  }
}

@@ -1689,28 +1689,24 @@
  state;%0A    
-if (
 myState.TAB_
@@ -1716,63 +1716,18 @@
 TES.
-get(MAIN_LOG_ID) == null) %7B%0A      myState.TAB_STATES.pu
+putIfAbsen
 t(MA
@@ -1773,22 +1773,16 @@
 ate());%0A
-    %7D%0A
   %7D%0A%0A  p
@@ -1859,12 +1859,8 @@
     
-if (
 mySt
@@ -1878,54 +1878,18 @@
 TES.
-get(id) == null) %7B%0A      myState.TAB_STATES.pu
+putIfAbsen
 t(id
@@ -1927,22 +1927,16 @@
 ate());%0A
-    %7D%0A
     retu

replace if with putIfAbsent