implemented into existing class

This commit is contained in:
gorodeckii 2017-07-05 14:48:50 +03:00
parent 3b1e944bf4
commit c6fd3f738c
2 changed files with 10 additions and 51 deletions

View File

@ -35,10 +35,17 @@ public class LoganSquareEnumConverter<T extends Enum & LoganSquareEnum> extends
@NonNull
private final T[] enumValues;
@Nullable
private final T defaultValue;
public LoganSquareEnumConverter(@NonNull final T[] enumValues) {
this(enumValues, null);
}
public LoganSquareEnumConverter(@NonNull final T[] enumValues, @Nullable final T defaultValue) {
super();
this.enumValues = enumValues;
this.defaultValue = defaultValue;
}
@Nullable
@ -52,6 +59,9 @@ public class LoganSquareEnumConverter<T extends Enum & LoganSquareEnum> extends
return value;
}
}
if (defaultValue != null) {
return defaultValue;
}
throw new ShouldNotHappenException();
}

View File

@ -1,51 +0,0 @@
/*
* Copyright (c) 2017 Touch Instinct
*
* This file is part of RoboSwag library.
*
* 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 ru.touchin.templates.logansquare;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import ru.touchin.roboswag.core.utils.ShouldNotHappenException;
/**
* Created by Anton Arhipov.
* LoganSquare converter from String to Enum with default value.
* Doesn't throw exception for unknown values.
*/
public class LoganSquareEnumConverterWithDefault<T extends Enum & LoganSquareEnum> extends LoganSquareEnumConverter<T> {
private final T defaultValue;
public LoganSquareEnumConverterWithDefault(@NonNull final T[] enumValues, @NonNull final T defaultValue) {
super(enumValues);
this.defaultValue = defaultValue;
}
@Nullable
@Override
public T getFromString(@Nullable final String string) {
try {
return super.getFromString(string);
} catch (final ShouldNotHappenException exception) {
return defaultValue;
}
}
}