/*
 * Copyright 2012-2013 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 jetbrains.jetpad.model.id;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public abstract class BaseId {
  private static final char SEPARATOR = '$';

  private static final Map<String, String> ourNamesMap = new HashMap<String, String>();

  private static final Random ourRandom = new Random();

  private static long getFirstPart(String s) {
    int index = s.indexOf(SEPARATOR);
    if (index ==  -1) return Coder.decode(s);
    return Coder.decode(s.substring(0, index));
  }

  private static long getSecondPart(String s) {
    int index = s.indexOf(SEPARATOR);
    if (index ==  -1) return 0;
    return Coder.decode(s.substring(index + 1));
  }

  private long myId1;
  private long myId2;
  
  protected BaseId() {
    this(Math.abs(ourRandom.nextLong()), Math.abs(ourRandom.nextLong()), null);
  }

  protected BaseId(String id) {
    this(id, null);
  }

  protected BaseId(String id, String name) {
    this(getFirstPart(id), getSecondPart(id), name);
  }

  private BaseId(long id1, long id2, String name) {
    myId1 = id1;
    myId2 = id2;

    String id = getId();
    synchronized (ourNamesMap) {
      String oldName = ourNamesMap.get(id);

      if (oldName != null && name != null && !oldName.equals(name)) throw new IllegalStateException();

      if (name != null) {
        ourNamesMap.put(id, name);
      }
    }
  }

  public String getId() {
    if (myId2 == 0) return Coder.encode(myId1);
    return Coder.encode(myId1) + SEPARATOR + Coder.encode(myId2);
  }

  public String getName() {
    synchronized (ourNamesMap) {
      return ourNamesMap.get(getId());
    }
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) return false;
    if (obj.getClass() != getClass()) return false;
    
    BaseId otherId = (BaseId) obj;

    if (myId1 == otherId.myId1 && myId2 == otherId.myId2) {
      return true;
    }

    return false;
  }

  @Override
  public int hashCode() {
    return (int) (myId1 ^ (myId2 << 32));
  }

  @Override
  public String toString() {
    synchronized (ourNamesMap) {
      String name = ourNamesMap.get(getId());
      if (name != null) {
        return name + " [" + getId() + "]";
      }
      return getId();
    }
  }
}

@@ -631,16 +631,56 @@
 el.id;%0A%0A
+import com.google.common.base.Objects;%0A%0A
 import j
@@ -982,400 +982,21 @@
 ate 
-static long getFirstPart(String s) %7B%0A    int index = s.indexOf(SEPARATOR);%0A    if (index ==  -1) return Coder.decode(s);%0A    return Coder.decode(s.substring(0, index));%0A  %7D%0A%0A  private static long getSecondPart(String s) %7B%0A    int index = s.indexOf(SEPARATOR);%0A    if (index ==  -1) return 0;%0A    return Coder.decode(s.substring(index + 1));%0A  %7D%0A%0A  private lo
+Stri
 ng myId
-1
 ;%0A
-  private long myId2;%0A  
 %0A  p
@@ -1206,126 +1206,16 @@
 ) %7B%0A
-    this(getFirstPart(id), getSecondPart(id), name);%0A  %7D%0A%0A  private BaseId(long id1, long id2, String name) %7B%0A
     myId
 1 = 
@@ -1214,58 +1214,13 @@
 myId
-1
  = id
-1;%0A    myId2 = id2;%0A%0A    String id = getId()
 ;%0A  
@@ -1482,29 +1482,57 @@
   %7D%0A%0A  p
-ublic
+rivate BaseId(long id1, long id2,
  String 
 getId() 
@@ -1527,22 +1527,20 @@
 ing 
-getId(
+name
 ) %7B%0A    
 if (
@@ -1539,62 +1539,43 @@
     
-if (myId2 == 0) return Coder.encode(myId1);%0A    return
+this(id2 == 0 ? Coder.encode(id1) :
  Cod
@@ -1584,19 +1584,17 @@
 .encode(
-myI
+i
 d1) + SE
@@ -1616,22 +1616,75 @@
 .encode(
+id2), name);%0A  %7D%0A%0A  public String getId() %7B%0A    return 
 myId
-2)
 ;%0A  %7D%0A%0A 
@@ -1930,20 +1930,16 @@
  false;%0A
-    
 %0A    Bas
@@ -1975,106 +1975,54 @@
     
-if (myId1 == otherId.myId1 && myId2 == otherId.myId2) %7B%0A      return true;%0A    %7D%0A%0A    return false
+return Objects.equal(getId(), otherId.getId())
 ;%0A  
@@ -2077,36 +2077,25 @@
 urn 
-(int) (myId1 %5E (myId2 %3C%3C 32)
+getId().hashCode(
 );%0A 

use string based ids