From 3b1e944bf49b42d481067ebd2d68180e1f84f62b Mon Sep 17 00:00:00 2001 From: gorodeckii Date: Wed, 5 Jul 2017 14:41:21 +0300 Subject: [PATCH 1/2] enum converter with default value --- .../LoganSquareEnumConverterWithDefault.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java diff --git a/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java b/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java new file mode 100644 index 0000000..1cbeec0 --- /dev/null +++ b/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java @@ -0,0 +1,51 @@ +/* + * 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 extends LoganSquareEnumConverter { + + 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; + } + } + +} From c6fd3f738c3cf4dfb93ab72a4aa8dd91ea5d7dc5 Mon Sep 17 00:00:00 2001 From: gorodeckii Date: Wed, 5 Jul 2017 14:48:50 +0300 Subject: [PATCH 2/2] implemented into existing class --- .../logansquare/LoganSquareEnumConverter.java | 10 ++++ .../LoganSquareEnumConverterWithDefault.java | 51 ------------------- 2 files changed, 10 insertions(+), 51 deletions(-) delete mode 100644 src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java diff --git a/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverter.java b/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverter.java index b10f32a..f94921a 100644 --- a/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverter.java +++ b/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverter.java @@ -35,10 +35,17 @@ public class LoganSquareEnumConverter 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 extends return value; } } + if (defaultValue != null) { + return defaultValue; + } throw new ShouldNotHappenException(); } diff --git a/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java b/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java deleted file mode 100644 index 1cbeec0..0000000 --- a/src/main/java/ru/touchin/templates/logansquare/LoganSquareEnumConverterWithDefault.java +++ /dev/null @@ -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 extends LoganSquareEnumConverter { - - 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; - } - } - -}