/*
 * Copyright 2000-2017 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.components.impl.stores;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.xmlb.XmlSerializer;
import org.jdom.Element;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings({"deprecation"})
public class DefaultStateSerializer {
  private static final Logger LOG = Logger.getInstance(DefaultStateSerializer.class);

  private DefaultStateSerializer() {
  }

  @SuppressWarnings({"unchecked"})
  @Nullable
  public static <T> T deserializeState(@Nullable Element stateElement, Class <T> stateClass, @Nullable T mergeInto) {
    if (stateElement == null) {
      return mergeInto;
    }
    else if (stateClass == Element.class) {
      return (T)stateElement;
    }
    else if (JDOMExternalizable.class.isAssignableFrom(stateClass)) {
      if (mergeInto != null) {
        String elementText = JDOMUtil.writeElement(stateElement);
        LOG.error("State is " + stateClass.getName() + ", merge into is " + mergeInto.toString() + ", state element text is " + elementText);
      }

      T t = ReflectionUtil.newInstance(stateClass);
      ((JDOMExternalizable)t).readExternal(stateElement);
      return t;
    }
    else if (mergeInto == null) {
      return XmlSerializer.deserialize(stateElement, stateClass);
    }
    else {
      XmlSerializer.deserializeInto(mergeInto, stateElement);
      return mergeInto;
    }
  }
}

@@ -645,17 +645,16 @@
 l.stores
-;
 %0A%0Aimport
@@ -692,17 +692,16 @@
 c.Logger
-;
 %0Aimport 
@@ -744,17 +744,16 @@
 alizable
-;
 %0Aimport 
@@ -782,25 +782,24 @@
 til.JDOMUtil
-;
 %0Aimport com.
@@ -826,17 +826,16 @@
 tionUtil
-;
 %0Aimport 
@@ -870,17 +870,16 @@
 rializer
-;
 %0Aimport 
@@ -898,101 +898,16 @@
 ment
-;%0Aimport org.jetbrains.annotations.Nullable;%0A%0A@SuppressWarnings(%7B%22deprecation%22%7D)%0Apublic class
+%0A%0Aobject
  Def
@@ -942,27 +942,11 @@
 ate 
-static final Logger
+val
  LOG
@@ -993,128 +993,32 @@
 izer
-.
+::
 class
-);%0A%0A  private DefaultStateSerializer() %7B%0A  %7D%0A%0A  @SuppressWarnings(%7B%22unchecked%22%7D)%0A  @Nullable%0A  public static
+.java)%0A%0A  fun
  %3CT%3E
- T
  des
@@ -1035,73 +1035,53 @@
 ate(
-@Nullable 
+state
 Element
+:
  
-state
 Element
+?
 , 
-Class %3CT%3E stateClass, @Nullable T
+stateClass: Class%3CT%3E,
  mer
@@ -1086,17 +1086,25 @@
 ergeInto
-)
+: T?): T?
  %7B%0A    i
@@ -1144,33 +1144,32 @@
 return mergeInto
-;
 %0A    %7D%0A    else 
@@ -1193,22 +1193,28 @@
  Element
-.
+::
 class
+.java
 ) %7B%0A    
@@ -1226,11 +1226,8 @@
 urn 
-(T)
 stat
@@ -1234,17 +1234,22 @@
 eElement
-;
+ as T?
 %0A    %7D%0A 
@@ -1282,15 +1282,21 @@
 able
-.
+::
 class.
+java.
 isAs
@@ -1362,22 +1362,19 @@
         
-String
+val
  element
@@ -1411,25 +1411,24 @@
 tateElement)
-;
 %0A        LOG
@@ -1463,17 +1463,12 @@
 ass.
-getN
+n
 ame
-()
  + %22
@@ -1551,17 +1551,16 @@
 entText)
-;
 %0A      %7D
@@ -1567,17 +1567,19 @@
 %0A%0A      
-T
+val
  t = Ref
@@ -1617,17 +1617,16 @@
 ass)
-;
 %0A      (
 (JDO
@@ -1621,17 +1621,21 @@
 %0A      (
-(
+t as 
 JDOMExte
@@ -1645,18 +1645,16 @@
 lizable)
-t)
 .readExt
@@ -1664,33 +1664,32 @@
 al(stateElement)
-;
 %0A      return t;
@@ -1687,17 +1687,16 @@
 return t
-;
 %0A    %7D%0A 
@@ -1792,17 +1792,16 @@
 teClass)
-;
 %0A    %7D%0A 
@@ -1870,17 +1870,16 @@
 Element)
-;
 %0A      r
@@ -1893,17 +1893,16 @@
 ergeInto
-;
 %0A    %7D%0A 

convert DefaultStateSerializer to kotlin