All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.util.xml.NamedEnumUtil Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition dom-openapi library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2014 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.util.xml;

import com.intellij.openapi.util.Comparing;
import com.intellij.util.Function;
import com.intellij.util.ReflectionUtil;

/**
 * @author peter
 */
public class NamedEnumUtil {
  private static final Function NAMED_SHOW = new Function() {
    @Override
    public String fun(final Enum s) {
      return ((NamedEnum) s).getValue();
    }
  };
  private static final Function SIMPLE_SHOW = new Function() {
    @Override
    public String fun(final Enum s) {
      return s.name();
    }
  };
  
  public static  T getEnumElementByValue(final Class enumClass, final String value, Function show) {
    for (final T t : enumClass.getEnumConstants()) {
      if (Comparing.equal(value, show.fun(t))) {
        return t;
      }
    }
    return null;
  }
  public static  T getEnumElementByValue(final Class enumClass, final String value) {
    return getEnumElementByValue(enumClass, value, getShow(enumClass));
  }

  private static  Function getShow(final Class enumClass) {
    return ReflectionUtil.isAssignable(NamedEnum.class, enumClass) ? NAMED_SHOW : SIMPLE_SHOW;
  }

  public static  String getEnumValueByElement(final T element) {
    return element == null ? null : getShow(element.getClass()).fun(element);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy