forked from realtimetech-solution/opack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default value provider and annotation
- Loading branch information
1 parent
8f0eeb9
commit 9c6f15b
Showing
11 changed files
with
441 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/realtimetech/opack/annotation/DefaultValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (C) 2021 REALTIMETECH All Rights Reserved | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, 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 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE file that accompanied this code. | ||
* 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.realtimetech.opack.annotation; | ||
|
||
import com.realtimetech.opack.provider.DefaultValueProvider; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Fields annotated with @DefaultValue will not be serialized and deserialized | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ | ||
ElementType.FIELD, | ||
}) | ||
public @interface DefaultValue { | ||
/** | ||
* Return the default value provider for field | ||
* | ||
* @return the default value provider class | ||
*/ | ||
@NotNull Class<? extends DefaultValueProvider> provider(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/realtimetech/opack/provider/DefaultValueProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2021 REALTIMETECH All Rights Reserved | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, 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 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE file that accompanied this code. | ||
* 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.realtimetech.opack.provider; | ||
|
||
import com.realtimetech.opack.Opacker; | ||
import com.realtimetech.opack.bake.BakedType; | ||
import com.realtimetech.opack.exception.DeserializeException; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface DefaultValueProvider { | ||
/** | ||
* Deserialize opack value | ||
* | ||
* @param opacker the opacker | ||
* @param object the property owner | ||
* @param property the property to provide default value | ||
* @return the default value | ||
*/ | ||
@Nullable Object provide(@NotNull Opacker opacker, @NotNull Object object, @NotNull BakedType.Property property); | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/com/realtimetech/opack/provider/DefaultValueProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright (C) 2021 REALTIMETECH All Rights Reserved | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, 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 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE file that accompanied this code. | ||
* 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.realtimetech.opack.provider; | ||
|
||
import com.realtimetech.opack.Opacker; | ||
import com.realtimetech.opack.util.ReflectionUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.HashMap; | ||
|
||
public class DefaultValueProviderFactory { | ||
private final @NotNull Opacker opacker; | ||
|
||
private final @NotNull HashMap<@NotNull Class<? extends DefaultValueProvider>, @NotNull DefaultValueProvider> defaultValueProviderMap; | ||
|
||
/** | ||
* Constructs a TransformerFactory with the opacker | ||
* | ||
* @param opacker the opacker | ||
*/ | ||
public DefaultValueProviderFactory(@NotNull Opacker opacker) { | ||
this.opacker = opacker; | ||
|
||
this.defaultValueProviderMap = new HashMap<>(); | ||
} | ||
|
||
/** | ||
* Returns default value provider instance | ||
* | ||
* @param defaultValueProviderType the default value provider class | ||
* @return the default value provider instance | ||
* @throws InstantiationException if the default value provider class object cannot be instantiated; if the default value provider is not in the default value provider class | ||
*/ | ||
public <P extends DefaultValueProvider> @NotNull P get(@NotNull Class<P> defaultValueProviderType) throws InstantiationException { | ||
if (!this.defaultValueProviderMap.containsKey(defaultValueProviderType)) { | ||
synchronized (this.defaultValueProviderMap) { | ||
if (!this.defaultValueProviderMap.containsKey(defaultValueProviderType)) { | ||
P instance = null; | ||
|
||
try { | ||
// Create instance using DefaultValueProvider(Opacker) constructor | ||
try { | ||
instance = ReflectionUtil.createInstance(defaultValueProviderType, this.opacker); | ||
} catch (IllegalArgumentException exception) { | ||
// Ok, let's find no parameter constructor | ||
} | ||
|
||
// Create instance using DefaultValueProvider() constructor | ||
try { | ||
if (instance == null) { | ||
instance = ReflectionUtil.createInstance(defaultValueProviderType); | ||
} | ||
} catch (IllegalArgumentException exception) { | ||
// Ok, let's throw exception | ||
} | ||
} catch (InvocationTargetException | IllegalAccessException exception) { | ||
InstantiationException instantiationException = new InstantiationException(defaultValueProviderType.getSimpleName() + " default value provider can't instantiation."); | ||
instantiationException.initCause(exception); | ||
|
||
throw instantiationException; | ||
} | ||
|
||
if (instance == null) { | ||
throw new InstantiationException(defaultValueProviderType.getSimpleName() + " default value provider must be implemented constructor(Opacker) or constructor()."); | ||
} | ||
|
||
this.defaultValueProviderMap.put(defaultValueProviderType, instance); | ||
} | ||
} | ||
} | ||
|
||
return defaultValueProviderType.cast(this.defaultValueProviderMap.get(defaultValueProviderType)); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/realtimetech/opack/provider/impl/BooleanFalseProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2023 REALTIMETECH All Rights Reserved | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, 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 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE file that accompanied this code. | ||
* 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.realtimetech.opack.provider.impl; | ||
|
||
import com.realtimetech.opack.Opacker; | ||
import com.realtimetech.opack.bake.BakedType; | ||
import com.realtimetech.opack.provider.DefaultValueProvider; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class BooleanFalseProvider implements DefaultValueProvider { | ||
/** | ||
* Deserialize opack value | ||
* | ||
* @param opacker the opacker | ||
* @param object the property owner | ||
* @param property the property to provide default value | ||
* @return the default value | ||
*/ | ||
@Override | ||
public @Nullable Object provide(@NotNull Opacker opacker, @NotNull Object object, BakedType.@NotNull Property property) { | ||
return false; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/realtimetech/opack/provider/impl/BooleanTrueProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (C) 2023 REALTIMETECH All Rights Reserved | ||
* | ||
* Licensed either under the Apache License, Version 2.0, or (at your option) | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation (subject to the "Classpath" exception), | ||
* either version 2, or any later version (collectively, 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 | ||
* http://www.gnu.org/licenses/ | ||
* http://www.gnu.org/software/classpath/license.html | ||
* | ||
* or as provided in the LICENSE file that accompanied this code. | ||
* 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.realtimetech.opack.provider.impl; | ||
|
||
import com.realtimetech.opack.Opacker; | ||
import com.realtimetech.opack.bake.BakedType; | ||
import com.realtimetech.opack.provider.DefaultValueProvider; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class BooleanTrueProvider implements DefaultValueProvider { | ||
/** | ||
* Deserialize opack value | ||
* | ||
* @param opacker the opacker | ||
* @param object the property owner | ||
* @param property the property to provide default value | ||
* @return the default value | ||
*/ | ||
@Override | ||
public @Nullable Object provide(@NotNull Opacker opacker, @NotNull Object object, BakedType.@NotNull Property property) { | ||
return true; | ||
} | ||
} |
Oops, something went wrong.