Translate value to enum instance.

        If value is already enum instance, check if this value belongs to base
        enum.
501175
def translate(self, value):
        
        if self._check_if_already_proper(value):
            return value

        try:
            return self.search_table[value]
        except KeyError:
            raise ValueError("Value {value} doesn't match any state.".format(
                value=value
            ))
437793
def to_xml(cls, enum_val):
        
        if enum_val not in cls._member_to_xml:
            raise ValueError(
                "value '%s' not in enumeration %s" % (enum_val, cls.__name__)
            )
        return cls._member_to_xml[enum_val]
914157
public static int getEnumValue(Enum en) {
        if (en != null) {
            int toCompareValue;
            if (en instanceof EnumReadable) {
                toCompareValue = ((EnumReadable) en).value();
            } else {
                toCompareValue = en.ordinal();
            }
            return toCompareValue;
        }

        return -1;
    }
1259441
protected <T> T convertIfNecessary(String value, T originalValue) {
        if (originalValue == null) {
            return (T) value;
        }

        return TypeConversionUtils.convertIfNecessary(value, (Class<T>) originalValue.getClass());
    }
289364
def validate_available_choice(enum, to_value):
    
    if to_value is None:
        return

    if type(to_value) is not int:
        try:
            to_value = int(to_value)
        except ValueError:
            message_str = "'{value}' cannot be converted to int"
            message = _(six.text_type(message_str))
            raise InvalidStatusOperationError(message.format(value=to_value))

    if to_value not in list(dict(enum.choices()).keys()):
        message = _(six.text_type('Select a valid choice. {value} is not one of the available choices.'))
        raise InvalidStatusOperationError(message.format(value=to_value))
349051
final public function equals($value)
    {
        if ($value instanceof Enum) {
            return $this->value == $value->getValue();
        }
        return $this->value == $value;
    }
1308039
public static <T extends Enum<T>> EnumExpression<T> asEnum(T value) {
        return asEnum(constant(value));
    }
1017938
public static Object convert(final Object value, Class<?> targetType, final String name) {
        if (value == null) {
            if (targetType.equals(Boolean.TYPE)) {
                return false;
            }
            return value;
        }

        final Class<? extends Object> actualType = value.getClass();

        if (targetType.isPrimitive()) {
            targetType =  PrimitiveTypes.valueOf(targetType.toString().toUpperCase()).getWraper();
        }

        if (targetType.isAssignableFrom(actualType)) {
            return value;
        }

        if (Number.class.isAssignableFrom(actualType) && Number.class.isAssignableFrom(targetType)) {
            return value;
        }

        if (!(value instanceof String)) {
            final String message = String.format("Expected type '%s' for '%s'. Found '%s'", targetType.getName(), name, actualType.getName());
            throw new IllegalArgumentException(message);
        }

        final String stringValue = (String) value;

        if (Enum.class.isAssignableFrom(targetType)) {
            final Class<? extends Enum> enumType = (Class<? extends Enum>) targetType;
            try {
                return Enum.valueOf(enumType, stringValue);
            } catch (final IllegalArgumentException e) {
                try {
                    return Enum.valueOf(enumType, stringValue.toUpperCase());
                } catch (final IllegalArgumentException e1) {
                    return Enum.valueOf(enumType, stringValue.toLowerCase());
                }
            }
        }

        try {
            // Force static initializers to run
            Class.forName(targetType.getName(), true, targetType.getClassLoader());
        } catch (final ClassNotFoundException e) {
            e.printStackTrace();
        }

        final PropertyEditor editor = Editors.get(targetType);

        if (editor == null) {
            final Object result = create(targetType, stringValue);
            if (result != null) {
                return result;
            }
        }

        if (editor == null) {
            final String message = String.format("Cannot convert to '%s' for '%s'. No PropertyEditor", targetType.getName(), name);
            throw new IllegalArgumentException(message);
        }

        editor.setAsText(stringValue);
        return editor.getValue();
    }
630079
def _call_from_base_type(self, value):
    
    methods = self._find_methods('_from_base_type', reverse=True)
    call = self._apply_list(methods)
    return call(value)
831478
def validate(self, value, messages=None):
        
        valid     = True
        primitive = value

        def log(msg):
            if messages is not None:
                messages.append(msg)

        if self.enum:
            if value not in self.enum.keys():
                valid = False
                args  = (self.name, str(value))
                log("%s value '%s' not in allowed enumerated values." % args)
            else:
                primitive = int(self.enum[value])

        if self.type:
            if self.type.validate(primitive, messages, self.name) is False:
                valid = False

        if self.range:
            if primitive < self.range[0] or primitive > self.range[1]:
                valid = False
                args  = (self.name, str(primitive), self.range[0], self.range[1])
                log("%s value '%s' out of range [%d, %d]." % args)

        return valid
1093765
def convert(self, value, view):
        
        if (SUPPORTS_ENUM and isinstance(self.choices, type)
                and issubclass(self.choices, enum.Enum)):
            try:
                return self.choices(value)
            except ValueError:
                self.fail(
                    u'must be one of {0!r}, not {1!r}'.format(
                        [c.value for c in self.choices], value
                    ),
                    view
                )

        if value not in self.choices:
            self.fail(
                u'must be one of {0!r}, not {1!r}'.format(
                    list(self.choices), value
                ),
                view
            )

        if isinstance(self.choices, abc.Mapping):
            return self.choices[value]
        else:
            return value
434055
def translate_value(value)
      case value
        when Hash
          self.class.new(value)
        when Array
          value.map { |element| translate_value(element) }
        else
          value
      end
    end
1288012
def can_use_enum(func):
    

    @wraps(func)
    def inner(self, value):
        if isinstance(value, Enum):
            return self.check_value(value.value) or func(self, value.value)

        return func(self, value)

    return inner
121569
def _opt_call_from_base_type(self, value):
    
    if isinstance(value, _BaseValue):
      value = self._call_from_base_type(value.b_val)
    return value
954504
def cast(self, value, **opts):
        
        if isinstance(value, self.result_type):
            return value
        if self._is_null(value):
            return None
        try:
            opts_ = self.opts.copy()
            opts_.update(opts)
            return self._cast(value, **opts_)
        except Exception as e:
            if not isinstance(e, ConverterError):
                e = ConverterError(None, exc=e)
            e.converter = self.__class__
            raise e
461000
static function ensureValue($value)
    {
        if (!self::hasValue($value)) {
            throw new InvalidValueException(sprintf(
                'The value %s is not defined in enum class "%s", known values: %s',
                self::dumpValue($value),
                static::class,
                implode(', ', array_map([static::class, 'dumpValue'], self::getValues()))
            ));
        }
    }
140398
def translate_enum(attribute, &block)
      builder = Builder.new(self, attribute, &block)

      # User.translated_status(:active)
      define_singleton_method(builder.method_name_singular) do |key|
        I18n.translate("#{builder.i18n_scope}.#{builder.i18n_location(key)}", default: builder.i18n_default_location(key))
      end

      # @return [Array]
      # @example
      #   f.select_field :gender, f.object.class.translated_genders
      define_singleton_method(builder.method_name_plural) do
        public_send(builder.enum_klass_method_name).map do |key, value|
          [public_send(builder.method_name_singular, key), key, value]
        end
      end

      # @return [String]
      # @example
      #   @user.translated_gender
      define_method(builder.method_name_singular) do
        if (key = public_send(builder.enum_instance_method_name)).present?
          self.class.public_send(builder.method_name_singular, key)
        end
      end
    end