/*
 * Copyright 2000-2009 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.openapi.module.impl;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModulePointer;
import org.jetbrains.annotations.NotNull;

/**
 * @author dsl
 */
public class ModulePointerImpl implements ModulePointer {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.module.impl.ModulePointerImpl");
  private Module myModule;
  private String myModuleName;

  ModulePointerImpl(Module module) {
    myModule = module;
    myModuleName = null;
  }

  ModulePointerImpl(String name) {
    myModule = null;
    myModuleName = name;
  }

  @Override
  public Module getModule() {
    return myModule;
  }

  @Override
  @NotNull
  public String getModuleName() {
    if (myModule != null) {
      return myModule.getName();
    }
    else {
      return myModuleName;
    }
  }

  void moduleAdded(Module module) {
    LOG.assertTrue(myModule == null);
    LOG.assertTrue(myModuleName.equals(module.getName()));
    myModuleName = null;
    myModule = module;
  }

  void moduleRemoved(Module module) {
    LOG.assertTrue(myModule == module);
    myModuleName = myModule.getName();
    myModule = null;
  }

}

@@ -634,17 +634,16 @@
 ule.impl
-;
 %0A%0Aimport
@@ -681,17 +681,16 @@
 c.Logger
-;
 %0Aimport 
@@ -723,17 +723,16 @@
 e.Module
-;
 %0Aimport 
@@ -776,17 +776,17 @@
 nter
-;
+%0A
 %0Aimport 
 org.
@@ -785,74 +785,53 @@
 ort 
-org.jetbrains.annotations.NotNull;%0A%0A/**%0A * @author dsl%0A */%0Apublic 
+java.util.concurrent.atomic.AtomicReference%0A%0A
 clas
@@ -854,18 +854,9 @@
 mpl 
-implements
+:
  Mod
@@ -868,16 +868,17 @@
 inter %7B%0A
+%0A
   privat
@@ -883,108 +883,45 @@
 ate 
-static final Logger LOG = Logger.getInstance(%22#com.intellij.openapi.module.impl.ModulePointerImpl%22);
+val myModule: AtomicReference%3CModule%3E
 %0A  p
@@ -927,22 +927,19 @@
 private 
-Module
+var
  myModul
@@ -943,70 +943,62 @@
 dule
-;%0A  private String myModuleName;%0A%0A  ModulePointerImpl(M
+Name: String? = null%0A%0A  internal constructor(m
 odule
- m
+: M
 odul
@@ -1017,23 +1017,39 @@
 odule = 
+AtomicReference(
 module
-;
+)
 %0A    myM
@@ -1064,25 +1064,24 @@
 e = null
-;
 %0A  %7D%0A%0A  
 ModulePo
@@ -1076,37 +1076,41 @@
 %0A%0A  
-ModulePointerImpl(
+internal constructor(name: 
 String
- name
 ) %7B%0A
@@ -1120,29 +1120,53 @@
  myModule = 
-null;
+AtomicReference%3CModule%3E(null)
 %0A    myModul
@@ -1177,25 +1177,24 @@
 e = name
-;
 %0A  %7D%0A%0A  
 @Overrid
@@ -1189,45 +1189,41 @@
 %0A%0A  
-@O
+o
 verride
-%0A  public 
+ fun get
 Module
- get
+(): 
 Module
-()
+?
  %7B%0A 
@@ -1240,25 +1240,30 @@
 myModule
-;
+.get()
 %0A  %7D%0A%0A  
 @Overrid
@@ -1258,93 +1258,79 @@
 %0A%0A  
-@O
+o
 verride
-%0A  @NotNull%0A  public String getModuleName() %7B%0A    if (myM
+ fun getModuleName(): String %7B%0A    val m
 odule 
-!
 = 
-null) %7B%0A  
+myModule.get()%0A
     
@@ -1341,56 +1341,22 @@
 rn m
-yM
 odule
-.getName();%0A    %7D%0A    else %7B%0A      return
+?.name ?:
  myM
@@ -1368,23 +1368,16 @@
 Name
-;%0A    %7D
 %0A  %7D%0A%0A  
 void
@@ -1368,28 +1368,36 @@
 Name%0A  %7D%0A%0A  
-void
+internal fun
  moduleAdded
@@ -1401,62 +1401,109 @@
 ded(
-M
+m
 odule
- m
+: M
 odule)
- %7B%0A    LOG.assertTrue(myModule == null);
+: Boolean %7B%0A    if (!myModule.compareAndSet(null, module)) %7B%0A      return false%0A    %7D%0A
 %0A   
@@ -1534,35 +1534,24 @@
 Name
-.equals(module.getName()));
+ == module.name)
 %0A   
@@ -1574,75 +1574,116 @@
 null
-;
 %0A    
-myModule = module;%0A  %7D%0A%0A  void moduleRemoved(Module module) %7B
+return true%0A  %7D%0A%0A  internal fun moduleRemoved(module: Module) %7B%0A    val resolvedModule = myModule.get()
 %0A   
@@ -1694,26 +1694,32 @@
 .assertTrue(
-my
+resolved
 Module == mo
@@ -1715,24 +1715,25 @@
 odule ==
+=
  module)
 ;%0A    my
@@ -1724,17 +1724,16 @@
  module)
-;
 %0A    myM
@@ -1748,52 +1748,151 @@
 e = 
-myModule.getName();%0A    myModule = null;
+resolvedModule.name%0A    myModule.set(null)%0A  %7D%0A%0A  companion object %7B%0A    private val LOG = Logger.getInstance(ModulePointerImpl::class.java)
 %0A  %7D%0A
-%0A
 %7D%0A

convert ModulePointerImpl to kotlin