From 9a7780703923db9d9b8110e891d5ef15c2487b4f Mon Sep 17 00:00:00 2001 From: indroneel Date: Sat, 3 Oct 2026 18:36:09 +0530 Subject: [PATCH] the first commit --- .editorconfig | 9 + .gitignore | 60 +++++ README.md | 4 +- moe-json/pom.xml | 22 ++ moe-main/pom.xml | 15 ++ .../main/java/daxi/moe/app/ConvertUsing.java | 39 ++++ .../src/main/java/daxi/moe/app/Converter.java | 30 +++ .../src/main/java/daxi/moe/app/External.java | 41 ++++ .../src/main/java/daxi/moe/app/Internal.java | 34 +++ .../src/main/java/daxi/moe/app/MapFrom.java | 32 +++ .../src/main/java/daxi/moe/app/MapTo.java | 30 +++ moe-main/src/main/java/daxi/moe/app/Moe.java | 71 ++++++ .../main/java/daxi/moe/codec/MoeCodec.java | 49 +++++ .../main/java/daxi/moe/codec/MoeDecoder.java | 34 +++ .../main/java/daxi/moe/codec/MoeEncoder.java | 42 ++++ .../main/java/daxi/moe/lib/ExternalImpl.java | 95 ++++++++ .../main/java/daxi/moe/lib/InternalImpl.java | 56 +++++ .../src/main/java/daxi/moe/lib/MoeImpl.java | 78 +++++++ .../java/daxi/moe/lib/m2j/M2JLinearImpl.java | 45 ++++ .../java/daxi/moe/lib/m2j/M2JMappedImpl.java | 45 ++++ .../main/java/daxi/moe/lib/m2j/M2JMethod.java | 206 ++++++++++++++++++ .../java/daxi/moe/lib/m2j/M2JObjectImpl.java | 139 ++++++++++++ .../java/daxi/moe/lib/m2j/M2JSimpleImpl.java | 73 +++++++ .../daxi/moe/lib/transform/M2JGlobals.java | 76 +++++++ .../daxi/moe/lib/transform/M2JLinear.java | 41 ++++ .../daxi/moe/lib/transform/M2JMapped.java | 41 ++++ .../daxi/moe/lib/transform/M2JObject.java | 49 +++++ .../daxi/moe/lib/transform/M2JSimple.java | 41 ++++ .../daxi/moe/lib/transform/M2JStatus.java | 28 +++ .../src/main/java/daxi/moe/model/AnyData.java | 56 +++++ .../main/java/daxi/moe/model/LinearData.java | 43 ++++ .../main/java/daxi/moe/model/MappedData.java | 48 ++++ .../main/java/daxi/moe/model/SimpleData.java | 56 +++++ .../META-INF/services/daxi.moe.app.Moe | 15 ++ moe-toml/pom.xml | 31 +++ moe-yaml/pom.xml | 31 +++ pom.xml | 90 ++++++++ 37 files changed, 1893 insertions(+), 2 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 moe-json/pom.xml create mode 100644 moe-main/pom.xml create mode 100644 moe-main/src/main/java/daxi/moe/app/ConvertUsing.java create mode 100644 moe-main/src/main/java/daxi/moe/app/Converter.java create mode 100644 moe-main/src/main/java/daxi/moe/app/External.java create mode 100644 moe-main/src/main/java/daxi/moe/app/Internal.java create mode 100644 moe-main/src/main/java/daxi/moe/app/MapFrom.java create mode 100644 moe-main/src/main/java/daxi/moe/app/MapTo.java create mode 100644 moe-main/src/main/java/daxi/moe/app/Moe.java create mode 100644 moe-main/src/main/java/daxi/moe/codec/MoeCodec.java create mode 100644 moe-main/src/main/java/daxi/moe/codec/MoeDecoder.java create mode 100644 moe-main/src/main/java/daxi/moe/codec/MoeEncoder.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/InternalImpl.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/MoeImpl.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/m2j/M2JLinearImpl.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/m2j/M2JMappedImpl.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/m2j/M2JMethod.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/transform/M2JGlobals.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/transform/M2JLinear.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/transform/M2JMapped.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/transform/M2JObject.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/transform/M2JSimple.java create mode 100644 moe-main/src/main/java/daxi/moe/lib/transform/M2JStatus.java create mode 100644 moe-main/src/main/java/daxi/moe/model/AnyData.java create mode 100644 moe-main/src/main/java/daxi/moe/model/LinearData.java create mode 100644 moe-main/src/main/java/daxi/moe/model/MappedData.java create mode 100644 moe-main/src/main/java/daxi/moe/model/SimpleData.java create mode 100644 moe-main/src/main/resources/META-INF/services/daxi.moe.app.Moe create mode 100644 moe-toml/pom.xml create mode 100644 moe-yaml/pom.xml create mode 100644 pom.xml diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..94e001b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +indent_style = tab +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..524dd64 --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +## OS Generated + +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db +Desktop.ini + +## Eclipse IDE + +.project +.classpath +.settings/ +.metadata +bin/ +*.tmp +*.bak +*.swp +local.properties +.externalToolBuilders/ +*.launch +.pmd +.eclipse-pmd + +# Intellij Idea +.idea +*.iml + +# Maven +target/ + +# Gradle +.gradle/ +build/ +out/ + +## Project specific +deploy + +# Default by github for Java projects + +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# Maven versionsBackup + +*.versionsBackup diff --git a/README.md b/README.md index 481b31b..474247f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# moe +# MOE: Mapping of Objects to External -A Java library to transfer state from external encoding formats (JSON, YAML) into Java data objects. And vice-versa. \ No newline at end of file +Moe is a Java library to transfer state from external encoding formats (JSON, YAML) into Java data objects. And vice-versa. \ No newline at end of file diff --git a/moe-json/pom.xml b/moe-json/pom.xml new file mode 100644 index 0000000..81c1be2 --- /dev/null +++ b/moe-json/pom.xml @@ -0,0 +1,22 @@ + + + + 4.0.0 + + daxi.moe + daxi.moe.parent + v1-SNAPSHOT + + daxi.moe.json + jar + + + + daxi.moe + daxi.moe.main + ${project.parent.version} + + + diff --git a/moe-main/pom.xml b/moe-main/pom.xml new file mode 100644 index 0000000..48cea1f --- /dev/null +++ b/moe-main/pom.xml @@ -0,0 +1,15 @@ + + + + 4.0.0 + + daxi.moe + daxi.moe.parent + v1-SNAPSHOT + + daxi.moe.main + jar + + diff --git a/moe-main/src/main/java/daxi/moe/app/ConvertUsing.java b/moe-main/src/main/java/daxi/moe/app/ConvertUsing.java new file mode 100644 index 0000000..93ba975 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/ConvertUsing.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.app; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Specifies the {@link Converter} implementation used to convert a field to and from + * {@link daxi.moe.model.AnyData}. + * + * @author indroneel + */ + +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ConvertUsing { + +/** + * Returns the converter implementation to use for the annotated field. + * + * @return the converter class + */ + Class value(); +} diff --git a/moe-main/src/main/java/daxi/moe/app/Converter.java b/moe-main/src/main/java/daxi/moe/app/Converter.java new file mode 100644 index 0000000..bb201c9 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/Converter.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.app; + +import daxi.moe.model.AnyData; + +import java.util.Optional; + +/** + * @author indroneel + */ + +public interface Converter { + + T decode(AnyData ad, Class type); + + Optional encode(Object obj); +} diff --git a/moe-main/src/main/java/daxi/moe/app/External.java b/moe-main/src/main/java/daxi/moe/app/External.java new file mode 100644 index 0000000..b48b2c1 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/External.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.app; + +import daxi.moe.model.AnyData; + +import java.io.InputStream; + +/** + * Context for external data sources, such as a byte array or an inputstream. The data is loaded + * from these sources, and then converted into Java entities. + * + * @author indroneel + */ + +public interface External { + + External from(InputStream in); + + External from(byte[] data); + + boolean hasPath(String path); + + AnyData getModel(); + + T convert(Class type); + + T convert(String path, Class type); +} diff --git a/moe-main/src/main/java/daxi/moe/app/Internal.java b/moe-main/src/main/java/daxi/moe/app/Internal.java new file mode 100644 index 0000000..cd67951 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/Internal.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.app; + +import daxi.moe.model.AnyData; + +import java.io.IOException; + +/** + * @author indroneel + */ + +public interface Internal { + + Internal from(Object obj); + + AnyData getModel(); + + byte[] convert() throws IOException; + + byte[] convert(boolean pretty) throws IOException; +} diff --git a/moe-main/src/main/java/daxi/moe/app/MapFrom.java b/moe-main/src/main/java/daxi/moe/app/MapFrom.java new file mode 100644 index 0000000..caa3c7f --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/MapFrom.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.app; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author indroneel + */ + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface MapFrom { + + String value(); +} diff --git a/moe-main/src/main/java/daxi/moe/app/MapTo.java b/moe-main/src/main/java/daxi/moe/app/MapTo.java new file mode 100644 index 0000000..cbb6410 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/MapTo.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.app; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author indroneel + */ + +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface MapTo { +} diff --git a/moe-main/src/main/java/daxi/moe/app/Moe.java b/moe-main/src/main/java/daxi/moe/app/Moe.java new file mode 100644 index 0000000..7862b30 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/app/Moe.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.app; + +import java.util.ServiceConfigurationError; +import java.util.ServiceLoader; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * @author indroneel + */ + +public abstract class Moe { + + private static final Logger L = Logger.getLogger(Moe.class.getName()); + + protected abstract Moe options(Options... opts); + + public abstract External external(String format); + + public abstract Internal internal(String format); + + public static Moe create() { + ServiceLoader loader = ServiceLoader.load(Moe.class); + try { + java.util.Optional moeopt = loader.findFirst(); + if (moeopt.isEmpty()) { + if(L.isLoggable(Level.SEVERE)) { + L.log(Level.SEVERE, "moe implementation NOT found"); + } + return null; + } + else { + Moe moe = moeopt.get(); + if(L.isLoggable(Level.CONFIG)) { + L.config(String.format("moe implementation found: %s", + moe.getClass().getName())); + } + return moe; + } + } + catch (ServiceConfigurationError err) { + if(L.isLoggable(Level.SEVERE)) { + L.log(Level.SEVERE, "moe implementation NOT found", err); + } + return null; + } + } + + public static Moe create(Options... opts) { + return Moe.create().options(opts); + } + + public static enum Options { + ANYCASE, + DECODE_IGNORE_MISSING + } +} diff --git a/moe-main/src/main/java/daxi/moe/codec/MoeCodec.java b/moe-main/src/main/java/daxi/moe/codec/MoeCodec.java new file mode 100644 index 0000000..13eeecd --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/codec/MoeCodec.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.codec; + +/** + * Supplies the format identifier and format-specific codec components for a supported external data + * format. + * + * @author indroneel + */ + +public interface MoeCodec { + +/** + * Returns the identifier of the format supported by this provider (e.g., "JSON", "XML"). + * + * @return the supported format identifier + */ + + String getFormat(); + +/** + * Returns a decoder for the format supported by this provider. + * + * @return a decoder for the supported format + */ + + MoeDecoder decoder(); + +/** + * Returns an encoder for the format supported by this provider. + * + * @return an encoder for the supported format + */ + + MoeEncoder encoder(); +} diff --git a/moe-main/src/main/java/daxi/moe/codec/MoeDecoder.java b/moe-main/src/main/java/daxi/moe/codec/MoeDecoder.java new file mode 100644 index 0000000..1efa2f8 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/codec/MoeDecoder.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.codec; + +import daxi.moe.app.Moe; +import daxi.moe.model.AnyData; + +import java.io.InputStream; +import java.util.List; + +/** + * @author indroneel + */ + +public interface MoeDecoder { + + void setSource(byte[] data); + + void setSource(InputStream input); + + AnyData decode(List options); +} diff --git a/moe-main/src/main/java/daxi/moe/codec/MoeEncoder.java b/moe-main/src/main/java/daxi/moe/codec/MoeEncoder.java new file mode 100644 index 0000000..024564f --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/codec/MoeEncoder.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.codec; + +import daxi.moe.model.AnyData; + +/** + * Encodes data into the byte representation of a supported format. + * + * @author indroneel + */ +public interface MoeEncoder { + + /** + * Encodes the supplied data using the default formatting. + * + * @param data the data to encode + * @return the encoded data as a byte array + */ + byte[] convert(AnyData data); + + /** + * Encodes the supplied data, optionally formatting the output for readability. + * + * @param data the data to encode + * @param pretty whether to format the output for readability + * @return the encoded data as a byte array + */ + byte[] convert(AnyData data, boolean pretty); +} diff --git a/moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java b/moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java new file mode 100644 index 0000000..15d6e06 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/ExternalImpl.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib; + +import daxi.moe.app.External; +import daxi.moe.app.Moe; +import daxi.moe.codec.MoeCodec; +import daxi.moe.lib.m2j.M2JLinearImpl; +import daxi.moe.lib.m2j.M2JMappedImpl; +import daxi.moe.lib.m2j.M2JObjectImpl; +import daxi.moe.lib.m2j.M2JSimpleImpl; +import daxi.moe.lib.transform.M2JGlobals; +import daxi.moe.lib.transform.M2JLinear; +import daxi.moe.lib.transform.M2JMapped; +import daxi.moe.lib.transform.M2JObject; +import daxi.moe.lib.transform.M2JSimple; +import daxi.moe.model.AnyData; + +import java.io.InputStream; +import java.util.List; + +/** + * @author indroneel + */ + +public class ExternalImpl implements External { + + private final MoeCodec mprov; + private final List options; + private final M2JGlobals globals; + + ExternalImpl(MoeCodec provider, List options) { + mprov = provider; + this.options = options; + globals = new M2JGlobals(options); + + M2JSimple m2jSim = new M2JSimpleImpl(globals); + globals.setM2J(m2jSim); + + M2JLinear m2jLin = new M2JLinearImpl(globals); + globals.setM2J(m2jLin); + + M2JMapped m2jMap = new M2JMappedImpl(globals); + globals.setM2J(m2jMap); + + M2JObject m2jObj = new M2JObjectImpl(globals); + globals.setM2J(m2jObj); + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // Methods of interface External + + @Override + public External from(InputStream in) { + return null; + } + + @Override + public External from(byte[] data) { + return null; + } + + @Override + public boolean hasPath(String path) { + return false; + } + + @Override + public AnyData getModel() { + return null; + } + + @Override + public T convert(Class type) { + return null; + } + + @Override + public T convert(String path, Class type) { + return null; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/InternalImpl.java b/moe-main/src/main/java/daxi/moe/lib/InternalImpl.java new file mode 100644 index 0000000..8e7b877 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/InternalImpl.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib; + +import daxi.moe.app.Internal; +import daxi.moe.app.Moe; +import daxi.moe.codec.MoeCodec; +import daxi.moe.model.AnyData; + +import java.io.IOException; +import java.util.List; + +/** + * + * @author indroneel + */ + +public class InternalImpl implements Internal { + + InternalImpl(MoeCodec provider, List options) { + //TODO + } + + @Override + public Internal from(Object obj) { + return null; + } + + @Override + public AnyData getModel() { + return null; + } + + @Override + public byte[] convert() throws IOException { + return new byte[0]; + } + + @Override + public byte[] convert(boolean pretty) throws IOException { + return new byte[0]; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/MoeImpl.java b/moe-main/src/main/java/daxi/moe/lib/MoeImpl.java new file mode 100644 index 0000000..4ec64d3 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/MoeImpl.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib; + +import daxi.moe.app.External; +import daxi.moe.app.Internal; +import daxi.moe.app.Moe; +import daxi.moe.codec.MoeCodec; + +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * @author indroneel + */ + +public class MoeImpl extends Moe { + + private static final Logger L = Logger.getLogger(MoeImpl.class.getName()); + + private final static Map providers; + + static { + if (L.isLoggable(Level.FINE)) { + L.fine("loading mapping providers"); + } + providers = new HashMap<>(); + ServiceLoader mpl = ServiceLoader.load(MoeCodec.class); + for (MoeCodec mp : mpl) { + String format = mp.getFormat(); + providers.put(format.toLowerCase(), mp); + if (L.isLoggable(Level.FINE)) { + L.fine(String.format("%s -> %s", format, mp.getClass().getName())); + } + } + } + + private List options; + + protected MoeImpl() { + options = new ArrayList<>(); + } + + @Override + public External external(String format) { + MoeCodec mp = providers.get(format.toLowerCase()); + if (mp != null) { + return new ExternalImpl(mp, options); + } + return null; + } + + @Override + public Internal internal (String format) { + return null; + } + + @Override + protected Moe options(Options... opts) { + if (opts != null && opts.length > 0) { + options = Arrays.asList(opts); + } + return this; + } +} \ No newline at end of file diff --git a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JLinearImpl.java b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JLinearImpl.java new file mode 100644 index 0000000..7c39403 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JLinearImpl.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.m2j; + +import daxi.moe.lib.transform.M2JGlobals; +import daxi.moe.lib.transform.M2JLinear; +import daxi.moe.lib.transform.M2JStatus; +import daxi.moe.model.MappedData; + +import java.lang.reflect.Method; + +/** + * The only implementation of the {@link M2JLinear} interface. + * + * @author indroneel + */ + +public class M2JLinearImpl implements M2JLinear { + + private final M2JGlobals globals; + + public M2JLinearImpl(M2JGlobals globals) { + this.globals = globals; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // Methods of interface M2JLinear + + @Override + public M2JStatus convert(MappedData data, Object targetObj, Method targetMthd) { + return M2JStatus.NOT_VALID; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JMappedImpl.java b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JMappedImpl.java new file mode 100644 index 0000000..c85aa7d --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JMappedImpl.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.m2j; + +import daxi.moe.lib.transform.M2JGlobals; +import daxi.moe.lib.transform.M2JMapped; +import daxi.moe.lib.transform.M2JStatus; +import daxi.moe.model.MappedData; + +import java.lang.reflect.Method; + +/** + * The only implementation of the {@link M2JMapped} interface. + * + * @author indroneel + */ + +public class M2JMappedImpl implements M2JMapped { + + private final M2JGlobals globals; + + public M2JMappedImpl(M2JGlobals globals) { + this.globals = globals; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // Methods of interface M2JMapped + + @Override + public M2JStatus convert(MappedData data, Object targetObj, Method targetMthd) { + return M2JStatus.NOT_VALID; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JMethod.java b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JMethod.java new file mode 100644 index 0000000..d71ebac --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JMethod.java @@ -0,0 +1,206 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.m2j; + +import daxi.moe.app.MapFrom; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Parameter; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; + +/** + * Contains information about a method on the target Java object. The method and the information + * can be used to transform some portions of internal data model and populate the containing object. + * + * @author indroneel + */ + +public class M2JMethod { + + private boolean valid = false; + private boolean setter = false; + private boolean linAdder = false; + private boolean mapAdder = false; + private String mapFromKey = null; + private Class paramType = null; + +/** + * Constructs the object around transforming method . The contained method may or may not be valid + * for transformation, depending on the method's signature and annotations. + * + * @param method the method to be encapsulated + */ + + public M2JMethod(Method method) { + + MapFrom mapFrom = method.getAnnotation(MapFrom.class); + if(mapFrom == null) { + // If the method does not have the @MapFrom annotation, it is not valid for + // transformation + return; + } + // populate the mapFromKey field with the value of the @MapFrom annotation + mapFromKey = mapFrom.value(); + if(mapFromKey == null || mapFromKey.isBlank()) { + // If the @MapFrom annotation does not have a valid key, it is not valid for + // transformation + return; + } + + int mod = method.getModifiers(); + if(Modifier.isAbstract(mod) || Modifier.isStatic(mod) || !Modifier.isPrivate(mod)) { + // If the method is abstract, static, or not private, it is not valid for transformation + return; + } + if(method.getReturnType() != Void.TYPE) { + // If the method does not return void, it is not valid for transformation + return; + } + + Parameter[] params = method.getParameters(); + + // Check if the method name starts with "_set" and has more than 4 characters + if(method.getName().startsWith("_set") && method.getName().length() > 4) { + if(params.length != 1) { + // If the method name starts with "_set" but does not have exactly one parameter, it + // is not valid for transformation + return; + } + + Type type = params[0].getParameterizedType(); + if(type instanceof TypeVariable || type instanceof ParameterizedType) { + // If the parameter type is a type variable or a parameterized type, it is not valid + // for transformation + return; + } + paramType = params[0].getType(); + if(paramType.isArray()) { + // If the parameter type is an array, it is not valid for transformation + return; + } + + valid = true; + setter = true; + } + // Check if the method name starts with "_add" and has more than 4 characters + else if(method.getName().startsWith("_add") && method.getName().length() > 4) { + if(params.length ==1 ) { + Type type = params[0].getParameterizedType(); + if(type instanceof TypeVariable || type instanceof ParameterizedType) { + // If the parameter type is a type variable or a parameterized type, it is not + // valid for transformation + return; + } + paramType = params[0].getType(); + if(paramType.isArray()) { + // If the parameter type is an array, it is not valid for transformation + return; + } + valid = true; + linAdder = true; + } + else if(params.length == 2) { + if(params[0].getType() != String.class) { + // If the first parameter type is not String, it is not valid for transformation + return; + } + Type type = params[1].getParameterizedType(); + if(type instanceof TypeVariable || type instanceof ParameterizedType) { + // If the second parameter type is a type variable or a parameterized type, it + // is not valid for transformation + return; + } + paramType = params[1].getType(); + if(paramType.isArray()) { + // If the second parameter type is an array, it is not valid for transformation + return; + } + valid = true; + mapAdder = true; + } + } + } + +/** + * Returns whether the method is valid for transformation. + * + * @return true if the method is valid for transformation, false otherwise. + */ + + boolean isValid() { + return valid; + } + +/** + * Returns whether the method is a setter. A setter is a method that takes a single parameter and + * sets it to the containing object. + * + * @return true if the method is a setter, false otherwise + */ + + boolean isSetter() { + return setter; + } + +/** + * Returns whether the method is a linear adder. A linear adder is a method that takes a single + * parameter and adds it to the containing object. The adder method is called multiple times, once + * for each value to be added. + * + * @return true if the method is a linear adder, false otherwise + */ + + boolean isLinearAdder() { + return linAdder; + } + +/** + * Returns whether the method is a mapped adder. A mapped adder is a method that takes two + * parameters, the first being a String key and the second being the value to be added. The library + * will use the key to determine which portion of the data model is mapped to the method. The adder + * method is called multiple times, once for each key-value pair to be added. + * + * @return true if the method is a mapped adder, false otherwise + */ + + boolean isMappedAdder() { + return mapAdder; + } + +/** + * Returns the key from the @MapFrom annotation. The library will use this key to determine which + * portion of the data model is mapped to the method. + * + * @return the key from the @MapFrom annotation + */ + + String getMapFromKey() { + return mapFromKey; + } + +/** + * Returns the parameter type of the method. The library will use this type to determine how to + * convert the data model item into the appropriate Java type. + * + * @return the parameter type of the method + */ + + Class getParamType() { + return paramType; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java new file mode 100644 index 0000000..9d34820 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JObjectImpl.java @@ -0,0 +1,139 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.m2j; + +import daxi.moe.app.MapFrom; +import daxi.moe.lib.transform.M2JGlobals; +import daxi.moe.lib.transform.M2JLinear; +import daxi.moe.lib.transform.M2JMapped; +import daxi.moe.lib.transform.M2JObject; +import daxi.moe.lib.transform.M2JSimple; +import daxi.moe.lib.transform.M2JStatus; +import daxi.moe.model.MappedData; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * The only implementation of the {@link M2JObject} interface. + * + * @author indroneel + */ + +public class M2JObjectImpl implements M2JObject { + + private static final Logger L = Logger.getLogger(M2JObjectImpl.class.getName()); + + private final M2JGlobals globals; + + public M2JObjectImpl(M2JGlobals globals) { + this.globals = globals; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // Methods of interface M2JObject + + @Override + public M2JStatus convert(MappedData data, Object targetObj, Method targetMthd) { + return null; + } + + @Override + public T convert(MappedData data, Class targetType) { + if(data == null || targetType == null) { + return null; + } + List methods = new LinkedList<>(); + collateAllMethods(targetType, methods); + if(methods.isEmpty()) { + return null; + } + + T target; + try { + Constructor ctor = targetType.getConstructor(); + target = ctor.newInstance(); + } + catch(Exception exep) { + if(L.isLoggable(Level.WARNING)) { + L.log(Level.WARNING, exep.getMessage(), exep); + } + return null; + } + + for(Method method : methods) { + M2JStatus m2jStat = transformImpl(data, target, method); + if(m2jStat == M2JStatus.ERROR) { + return null; + } + } + + return target; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // Helper methods + + private void collateAllMethods(Class type, List methodList) { + if(type.getClassLoader() == null) { + // Skip system classes + return; + } + + for (Method method : type.getDeclaredMethods()) { + if (!Modifier.isStatic(method.getModifiers())) { + MapFrom ann = method.getAnnotation(MapFrom.class); + if (ann != null && !ann.value().isBlank()) { + methodList.add(method); + } + } + } + + if (type.getSuperclass() != null) { + collateAllMethods(type.getSuperclass(), methodList); + } + } + + private M2JStatus transformImpl(MappedData data, Object targetObj, Method targetMthd) { + M2JSimple m2jSim = globals.getM2JSimple(); + M2JStatus m2jStat = m2jSim.convert(data, targetObj, targetMthd); + if(m2jStat != M2JStatus.NOT_VALID) { + // here: m2jStat == M2JStatus.SUCCESS or m2jStat == M2JStatus.ERROR + return m2jStat; + } + + M2JLinear m2jLin = globals.getM2JLinear(); + m2jStat = m2jLin.convert(data, targetObj, targetMthd); + if(m2jStat != M2JStatus.NOT_VALID) { + // here: m2jStat == M2JStatus.SUCCESS or m2jStat == M2JStatus.ERROR + return m2jStat; + } + + M2JMapped m2jMap = globals.getM2JMapped(); + m2jStat = m2jMap.convert(data, targetObj, targetMthd); + if(m2jStat != M2JStatus.NOT_VALID) { + // here: m2jStat == M2JStatus.SUCCESS or m2jStat == M2JStatus.ERROR + return m2jStat; + } + + // here: m2jStat == M2JStatus.NOT_VALID + return convert(data, targetObj, targetMthd); + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java new file mode 100644 index 0000000..0239646 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/m2j/M2JSimpleImpl.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.m2j; + +import daxi.moe.lib.transform.M2JGlobals; +import daxi.moe.lib.transform.M2JSimple; +import daxi.moe.lib.transform.M2JStatus; +import daxi.moe.model.MappedData; + +import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.math.BigInteger; + +/** + * The only implementation of the {@link daxi.moe.lib.transform.M2JSimple} interface. + * + * @author indroneel + */ + +public class M2JSimpleImpl implements M2JSimple { + + private final M2JGlobals globals; + + public M2JSimpleImpl(M2JGlobals globals) { + this.globals = globals; + } + + //////////////////////////////////////////////////////////////////////////////////////////////// + // Methods of interface M2JSimple + + @Override + public M2JStatus convert(MappedData data, Object target, Method method) { + + M2JMethod m2jMthd = new M2JMethod(method); + if(!m2jMthd.isValid() || !m2jMthd.isSetter()) { + return M2JStatus.NOT_VALID; + } + + Class paramType = m2jMthd.getParamType(); + if(!isSupportedParamType(paramType)) { + return M2JStatus.NOT_VALID; + } + + return M2JStatus.SUCCESS; + } + + private boolean isSupportedParamType(Class paramType) { + return paramType == String.class + || paramType == BigInteger.class + || paramType == BigDecimal.class + || Number.class.isAssignableFrom(paramType) + || paramType == byte.class + || paramType == short.class + || paramType == int.class + || paramType == long.class + || paramType == float.class + || paramType == double.class + || paramType == boolean.class + || paramType == Boolean.class; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/transform/M2JGlobals.java b/moe-main/src/main/java/daxi/moe/lib/transform/M2JGlobals.java new file mode 100644 index 0000000..ac9417d --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/transform/M2JGlobals.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.transform; + +import daxi.moe.app.Moe; + +import java.util.List; + +/** + * A registry for model-to-Java transformers as singletons. It also stores options that were set + * during instatiation of MOE. + * + * @author indroneel + */ + +public class M2JGlobals { + + private final List options; + + private M2JSimple simM2j; + private M2JLinear linM2j; + private M2JMapped mapM2j; + private M2JObject objM2j; + + public M2JGlobals(List options) { + this.options = options; + } + + public void setM2J(M2JSimple m2j) { + simM2j = m2j; + } + + public void setM2J(M2JLinear m2j) { + linM2j = m2j; + } + + public void setM2J(M2JMapped m2j) { + mapM2j = m2j; + } + + public void setM2J(M2JObject m2j) { + objM2j = m2j; + } + + public boolean hasOption(Moe.Options opt) { + return options.contains(opt); + } + + public M2JSimple getM2JSimple() { + return simM2j; + } + + public M2JLinear getM2JLinear() { + return linM2j; + } + + public M2JMapped getM2JMapped() { + return mapM2j; + } + + public M2JObject getM2JObject() { + return objM2j; + } +} diff --git a/moe-main/src/main/java/daxi/moe/lib/transform/M2JLinear.java b/moe-main/src/main/java/daxi/moe/lib/transform/M2JLinear.java new file mode 100644 index 0000000..79b9437 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/transform/M2JLinear.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.transform; + +import daxi.moe.model.MappedData; + +import java.lang.reflect.Method; + +/** + * Defines a marker interface for classes that can be used to populate portions of a Java object + * with {@link daxi.moe.model.LinearData}. + * + * @author indroneel + */ + +public interface M2JLinear { + +/** + * Converts a linear data from the given mapped data into the target object using the specified + * method. + * + * @param data The mapped data that contains the linear data to convert. + * @param targetObj The target object to populate with the converted data. + * @param targetMthd The method to use for populating the target object. + * @return The status of the conversion. + */ + + M2JStatus convert(MappedData data, Object targetObj, Method targetMthd); +} diff --git a/moe-main/src/main/java/daxi/moe/lib/transform/M2JMapped.java b/moe-main/src/main/java/daxi/moe/lib/transform/M2JMapped.java new file mode 100644 index 0000000..8513a70 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/transform/M2JMapped.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.transform; + +import daxi.moe.model.MappedData; + +import java.lang.reflect.Method; + +/** + * Defines a marker interface for classes that can be used to populate portions of a Java object + * with {@link daxi.moe.model.MappedData}. + * + * @author indroneel + */ + +public interface M2JMapped { + +/** + * Converts a nested mapped data contained within a given mapped data into the target object using + * the specified method. + * + * @param data The mapped data that contains the nested mapped data to convert. + * @param targetObj The target object to populate with the converted data. + * @param targetMthd The method to use for populating the target object. + * @return The status of the conversion. + */ + + M2JStatus convert(MappedData data, Object targetObj, Method targetMthd); +} diff --git a/moe-main/src/main/java/daxi/moe/lib/transform/M2JObject.java b/moe-main/src/main/java/daxi/moe/lib/transform/M2JObject.java new file mode 100644 index 0000000..88389fc --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/transform/M2JObject.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.transform; + +import daxi.moe.model.MappedData; + +import java.lang.reflect.Method; + +/** + * Defines a marker interface for classes that can be used to convert from + * {@link daxi.moe.model.MappedData} to a Java object. + * + * @author indroneel + */ + +public interface M2JObject { + +/** + * Populates a Java object with data from a {@link MappedData} instance using the specified method. + * @param data the {@link MappedData} instance containing the data to populate the Java object + * @param targetObj the Java object to populate + * @param targetMthd the method to use for populating the Java object + * @return the status of the operation + */ + + M2JStatus convert(MappedData data, Object targetObj, Method targetMthd); + +/** + * Constructs a Java object of the specified type from the given {@link MappedData} instance. + * @param data the {@link MappedData} instance containing the data to populate the Java object + * @param targetType the class of the Java object to construct + * @param the type of the Java object to construct + * @return the constructed Java object of the specified type + */ + + T convert(MappedData data, Class targetType); +} diff --git a/moe-main/src/main/java/daxi/moe/lib/transform/M2JSimple.java b/moe-main/src/main/java/daxi/moe/lib/transform/M2JSimple.java new file mode 100644 index 0000000..737e485 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/transform/M2JSimple.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.transform; + +import daxi.moe.model.MappedData; + +import java.lang.reflect.Method; + +/** + * Defines a marker interface for classes that can be used to populate portions of a Java object + * with {@link daxi.moe.model.SimpleData}. + * + * @author indroneel + */ + +public interface M2JSimple { + +/** + * Converts a linear data from the given mapped data into the target object using the specified + * method. + * + * @param data the mapped data that contains the simple data to convert + * @param targetObj the object to populate + * @param targetMthd the method to invoke on the target object to set the value + * @return The status of the conversion. + */ + + M2JStatus convert(MappedData data, Object targetObj, Method targetMthd); +} diff --git a/moe-main/src/main/java/daxi/moe/lib/transform/M2JStatus.java b/moe-main/src/main/java/daxi/moe/lib/transform/M2JStatus.java new file mode 100644 index 0000000..44f5dea --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/lib/transform/M2JStatus.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.lib.transform; + +/** + * Defines the possible statuses for a transformation. The transformation can be from the model data + * to Java objects or vice versa. + * + * @author indroneel + */ + +public enum M2JStatus { + NOT_VALID, + SUCCESS, + ERROR +} diff --git a/moe-main/src/main/java/daxi/moe/model/AnyData.java b/moe-main/src/main/java/daxi/moe/model/AnyData.java new file mode 100644 index 0000000..750e5c3 --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/model/AnyData.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.model; + +/** + * @author indroneel + */ + +public class AnyData { + + private final SimpleData simple; + private final LinearData linear; + private final MappedData mapped; + + public AnyData(SimpleData simple) { + this.simple = simple; + this.linear = null; + this.mapped = null; + } + + public AnyData(LinearData linear) { + this.simple = null; + this.linear = linear; + this.mapped = null; + } + + public AnyData(MappedData mapped) { + this.simple = null; + this.linear = null; + this.mapped = mapped; + } + + public SimpleData asSimple() { + return simple; + } + + public LinearData asLinear() { + return linear; + } + + public MappedData asMapped() { + return mapped; + } +} diff --git a/moe-main/src/main/java/daxi/moe/model/LinearData.java b/moe-main/src/main/java/daxi/moe/model/LinearData.java new file mode 100644 index 0000000..f9b2c3b --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/model/LinearData.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.model; + +import java.util.LinkedList; +import java.util.List; + +/** + * @author indroneel + */ + +public class LinearData { + + private final List dataList; + + public LinearData() { + dataList = new LinkedList<>(); + } + + public void add(AnyData value) { + dataList.add(value); + } + + public AnyData get(int position) { + return dataList.get(position); + } + + public int size() { + return dataList.size(); + } +} diff --git a/moe-main/src/main/java/daxi/moe/model/MappedData.java b/moe-main/src/main/java/daxi/moe/model/MappedData.java new file mode 100644 index 0000000..c7c1b9d --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/model/MappedData.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.model; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author indroneel + */ + +public class MappedData { + + private final Map dataMap; + + public MappedData() { + dataMap = new HashMap<>(); + } + + public List getKeys() { + return dataMap.keySet().stream().toList(); + } + + public boolean contains(String key) { + return dataMap.containsKey(key); + } + + public AnyData get(String key) { + return dataMap.get(key); + } + + public void put(String key, AnyData value) { + dataMap.put(key, value); + } +} diff --git a/moe-main/src/main/java/daxi/moe/model/SimpleData.java b/moe-main/src/main/java/daxi/moe/model/SimpleData.java new file mode 100644 index 0000000..3d55d8e --- /dev/null +++ b/moe-main/src/main/java/daxi/moe/model/SimpleData.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) DuxTechnica + * + * Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not + * use this file except in compliance with the License. You may obtain a copy of the License by + * contacting: + * + * Dux Technica, contact@duxtechnica.com + * + * This file 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 daxi.moe.model; + +/** + * @author indroneel + */ + +public class SimpleData { + + private final String strVal; + private final Number numVal; + private final Boolean boolVal; + + public SimpleData(String value) { + strVal = value; + numVal = null; + boolVal = null; + } + + public SimpleData(Number value) { + strVal = null; + numVal = value; + boolVal = null; + } + + public SimpleData(Boolean value) { + strVal = null; + numVal = null; + boolVal = value; + } + + public String getString() { + return strVal; + } + + public Boolean getBoolean() { + return boolVal; + } + + public Number getNumber() { + return numVal; + } +} diff --git a/moe-main/src/main/resources/META-INF/services/daxi.moe.app.Moe b/moe-main/src/main/resources/META-INF/services/daxi.moe.app.Moe new file mode 100644 index 0000000..d136625 --- /dev/null +++ b/moe-main/src/main/resources/META-INF/services/daxi.moe.app.Moe @@ -0,0 +1,15 @@ +# +# Copyright (c) DuxTechnica +# +# Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not +# use this file except in compliance with the License. You may obtain a copy of the License by +# contacting: +# +# Dux Technica, contact@duxtechnica.com +# +# This file 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. +# + +daxi.moe.lib.MoeImpl \ No newline at end of file diff --git a/moe-toml/pom.xml b/moe-toml/pom.xml new file mode 100644 index 0000000..405b098 --- /dev/null +++ b/moe-toml/pom.xml @@ -0,0 +1,31 @@ + + + + 4.0.0 + + daxi.moe + daxi.moe.parent + v1-SNAPSHOT + + daxi.moe.toml + jar + + + 1.0.0 + + + + + daxi.moe + daxi.moe.app + ${project.parent.version} + + + org.tomlj + tomlj + ${dep.tomlj.version} + + + diff --git a/moe-yaml/pom.xml b/moe-yaml/pom.xml new file mode 100644 index 0000000..695f3e5 --- /dev/null +++ b/moe-yaml/pom.xml @@ -0,0 +1,31 @@ + + + + 4.0.0 + + daxi.moe + daxi.moe.parent + v1-SNAPSHOT + + daxi.moe.yaml + jar + + + 2.4 + + + + + daxi.moe + daxi.moe.app + ${project.parent.version} + + + org.yaml + snakeyaml + ${dep.snakeyaml.version} + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fe33a1b --- /dev/null +++ b/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + daxi.moe + daxi.moe.parent + moe + Library to transfer state from external encoding formats (JSON, YAML) into Java data objects. + pom + v1-SNAPSHOT + + + 3.12.1 + 3.6.3 + 3.3.0 + 3.1.1 + 3.1.1 + 25 + + + + moe-main + moe-json + moe-yaml + moe-toml + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${plugin.compiler.version} + + ${target.java.version} + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${plugin.javadoc.version} + + + javadoc + package + + jar + + + + + ${java.home}/bin/javadoc + false + + + + org.apache.maven.plugins + maven-source-plugin + ${plugin.source.version} + + + attach-sources + verify + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-release-plugin + ${plugin.release.version} + + false + release + true + + + + org.apache.maven.plugins + maven-deploy-plugin + ${plugin.deploy.version} + + + + +