├── .gitignore
├── LICENSE
├── README.md
├── boxer
├── build.gradle
├── libs
│ └── google-play-services.jar
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── larswerkman
│ │ │ └── boxer
│ │ │ ├── Boxer.java
│ │ │ ├── Execution.java
│ │ │ ├── TypeAdapter.java
│ │ │ ├── annotations
│ │ │ ├── Adapter.java
│ │ │ ├── Box.java
│ │ │ ├── Deserialize.java
│ │ │ ├── Serialize.java
│ │ │ └── Wrap.java
│ │ │ ├── internal
│ │ │ ├── AdapterBinding.java
│ │ │ ├── AdaptersClass.java
│ │ │ ├── ArrayFieldBinding.java
│ │ │ ├── BoxClass.java
│ │ │ ├── BoxerProcessor.java
│ │ │ ├── FieldBinding.java
│ │ │ ├── GeneratedAdapters.java
│ │ │ ├── ListFieldBinding.java
│ │ │ └── MethodBinding.java
│ │ │ └── wrappers
│ │ │ └── android
│ │ │ ├── BundleWrapper.java
│ │ │ ├── DataMapWrapper.java
│ │ │ ├── ParcelWrapper.java
│ │ │ └── SQLiteWrapper.java
│ └── resources
│ │ └── META-INF
│ │ └── services
│ │ └── javax.annotation.processing.Processor
│ └── test
│ └── java
│ └── com
│ └── larswerkman
│ └── boxer
│ ├── AbstractWrapperTest.java
│ ├── BoxerTest.java
│ ├── adapters
│ └── DateTypeAdapter.java
│ ├── boxables
│ ├── AccessBoxable.java
│ ├── AdapterBoxable.java
│ ├── EnumBoxable.java
│ ├── InheritanceAccessBoxable.java
│ ├── InheritanceMultipleBoxable.java
│ ├── InheritancePrimaryBoxable.java
│ ├── ListBoxable.java
│ ├── NestedBoxable.java
│ ├── ObjectBoxable.java
│ ├── PrimaryBoxable.java
│ ├── SerializeBoxable.java
│ └── TransientBoxable.java
│ ├── enums
│ └── PrimaryEnum.java
│ └── wrappers
│ ├── TestWrapper.java
│ ├── TestWrapperTest.java
│ └── android
│ ├── BundleWrapperTest.java
│ ├── DataMapWrapperTest.java
│ ├── ParcelWrapperTest.java
│ └── SQLiteWrapperTest.java
├── build.gradle
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | #Eclipse
2 | .project
3 | .classpath
4 | .settings
5 |
6 | #IntelliJ IDEA
7 | .idea
8 | *.iml
9 | classes
10 |
11 | #Command line
12 | build.xml
13 | proguard-project.txt
14 |
15 | #Mac specific
16 | .DS_Store
17 |
18 | #User specific
19 | local.properties
20 | sample/src/com/cyrilmottier/android/polarissample/util/LocalConfig.java
21 |
22 | #gradle
23 | build
24 | .gradle
25 | gradle
26 | gradlew
27 | gradlew.bat
28 | signing.properties
29 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2014 Lars Werkman
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Boxer
2 | =====
3 |
4 | Annotation based serialization library for Java and Android frameworks,
5 | by generating boilerplate code.
6 |
7 | * Define classes as serializable with the @Box
annotation.
8 | * By default all fields will be serialized, except fields with the transient modifier
9 | * the @Wrap
annotation will wrap a List
fields into the appropriate Subclass. (Subclass needs to have a no-args constructor)
10 | * Default non-class serialization possible
11 |
12 | ```java
13 | @Box
14 | public class Example {
15 |
16 | private int height;
17 | private transient int width;
18 | public double weight;
19 |
20 | @Wrap(Stack.class)
21 | public List stack;
22 |
23 | //Empty constructor for Injection
24 | public Example(){
25 |
26 | }
27 |
28 | //Getters and Setters for private fields
29 | }
30 | ```
31 |
32 | Retrieving a Boxer
instance, and serializing and de-serializing your data.
33 |
34 | Example using the Bundle
class:
35 |
36 | ```java
37 | public class ExampleActivity extends Activity {
38 |
39 | @Override
40 | public void onSaveInstanceState(Bundle outState) {
41 | super.onSaveInstanceState(outState);
42 |
43 | Boxer boxer = Boxer.from(outState);
44 | boxer.add("Example", new Example());
45 | }
46 |
47 | @Override
48 | public void onCreate(Bundle savedInstanceState) {
49 | super.onCreate(savedInstanceState);
50 |
51 | Boxer boxer = Boxer.from(savedInstanceState);
52 | boxer.get("Example", Example.class);
53 | }
54 | }
55 | ```
56 |
57 | To register your own Boxer wrapper for a specific class:
58 |
59 | ```java
60 | //Registering a wrapper class for a specific Target class
61 | Boxer.registerWrapper(Wrapper.class, Target.class);
62 |
63 | //Removing a wrapper for a specific wrapper class
64 | Boxer.removeWrapper(Wrapper.class);
65 |
66 | //Removing a wrapper for a target class
67 | Boxer.removeWrapperForType(Target.class);
68 |
69 | //Clearing all the wrappers
70 | Boxer.clearWrappers();
71 | ```
72 |
73 | TypeAdapters define how an non-Boxable class should be serialized and deserialized.
74 | TypeAdapter will need to be registered with the @Adapter
annotation.
75 | The type parameter of the TypeAdapter should be the class you want to serialize / deserialize.
76 |
77 | ```java
78 | @Adapter
79 | public class DateTypeAdapter extends TypeAdapter {
80 |
81 | private static final String TIME_KEY = "time_key";
82 |
83 | @Override
84 | public void serialize(Boxer> boxer, Date object) {
85 | boxer.addLong(TIME_KEY, object.getTime());
86 | }
87 |
88 | @Override
89 | public Date deserialize (Boxer> boxer) {
90 | return new Date(boxer.getLong(TIME_KEY));
91 | }
92 | }
93 | ```
94 |
95 | @Serialize
and @Deserialize
are used to annotate methods that will be called after or before serialization or deserialization inside Boxable
classes.
96 |
97 | Both annotations take an Execution
enum as parameter to specify when they will be executed.
98 | Default execution behaviour will be after serialization or deserialization.
99 |
100 | The method can have a Boxer
parameter or be empty.
101 |
102 | ```java
103 | @Serialize
104 | public void serialization(Boxer> boxer){
105 | //Do something
106 | }
107 |
108 | @Deserialize(Execution.BEFORE)
109 | public void deserialization(Boxer> boxer){
110 | //Do something
111 | }
112 | ```
113 |
114 |
115 | Current supported supported classes:
116 |
117 | * (Android) Bundle
118 | * (Android) Parcel
119 | * (Android) SQLiteDatabase (Experimental)
120 | * (Java/Android) DataMap
121 |
122 | __Beta phase!__
123 |
124 | Proguard
125 | ----------
126 | ```groovy
127 | -dontwarn com.larswerkman.boxer.internal.**
128 | -dontwarn com.larswerkman.boxer.wrappers.**
129 | -keep class **$$Boxer { *; }
130 | -keep class **_TypeAdapter { *; }
131 | -keepnames class * { @com.larswerkman.boxer.annotations.Box *;}
132 | -keepnames class * { @com.larswerkman.boxer.annotations.Adapter *;}
133 | -keepclasseswithmembernames class * {
134 | @com.larswerkman.boxer.annotations.* ;
135 | }
136 | ```
137 |
138 | Dependency
139 | ----------
140 | Adding it as a dependency to your project.
141 |
142 | ```xml
143 |
144 | com.larswerkman
145 | boxer
146 | 0.4.2
147 |
148 | ```
149 |
150 | ```groovy
151 | dependencies {
152 | compile 'com.larswerkman:boxer:0.4.2'
153 | }
154 | ```
155 | License
156 | -------
157 |
158 | Copyright 2015 Lars Werkman
159 |
160 | Licensed under the Apache License, Version 2.0 (the "License");
161 | you may not use this file except in compliance with the License.
162 | You may obtain a copy of the License at
163 |
164 | http://www.apache.org/licenses/LICENSE-2.0
165 |
166 | Unless required by applicable law or agreed to in writing, software
167 | distributed under the License is distributed on an "AS IS" BASIS,
168 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169 | See the License for the specific language governing permissions and
170 | limitations under the License.
171 |
172 | Developed By
173 | **Lars Werkman**
--------------------------------------------------------------------------------
/boxer/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'maven'
2 | apply plugin: 'signing'
3 |
4 | dependencies {
5 | compile 'com.squareup:javapoet:1.2.0'
6 |
7 | //Testing dependency's
8 | testCompile group: 'junit', name: 'junit', version: '4.12'
9 | testCompile group: 'com.google.guava', name: 'guava', version: '12.0'
10 | testCompile 'org.assertj:assertj-core:2.0.0'
11 | testCompile 'org.robolectric:robolectric:3.0-rc2'
12 |
13 | //Provided dependency's to compile against
14 | provided 'com.google.android:android:4.1.1.4'
15 | provided fileTree(dir: 'libs', include: ['*.jar'])
16 | }
17 |
18 | group = 'com.larswerkman'
19 | version = '0.4.2'
20 |
21 | task javadocJar(type: Jar, dependsOn: javadoc) {
22 | classifier = 'javadoc'
23 | from 'build/docs/javadoc'
24 | }
25 |
26 | task sourcesJar(type: Jar) {
27 | from sourceSets.main.allSource
28 | classifier = 'sources'
29 | }
30 |
31 | artifacts {
32 | archives jar
33 | archives javadocJar
34 | archives sourcesJar
35 | }
36 |
37 | signing {
38 | sign configurations.archives
39 | }
40 |
41 | uploadArchives {
42 | repositories {
43 | mavenDeployer {
44 | beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
45 |
46 | repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
47 | authentication(userName: nexusUsername, password: nexusPassword)
48 | }
49 |
50 | pom.project {
51 | name 'Boxer'
52 | packaging 'jar'
53 | description 'Annotation based serialization for java and Android'
54 | url 'https://github.com/LarsWerkman/Boxer'
55 |
56 | scm {
57 | url 'scm:git@github.com:LarsWerkman/Boxer.git'
58 | connection 'scm:git@github.com:LarsWerkman/Boxer.git'
59 | developerConnection 'scm:git@github.com:LarsWerkman/Boxer.git'
60 | }
61 |
62 | licenses {
63 | license {
64 | name 'The Apache Software License, Version 2.0'
65 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
66 | distribution 'repo'
67 | }
68 | }
69 |
70 | developers {
71 | developer {
72 | id 'larswerkman'
73 | name 'Lars Werkman'
74 | }
75 | }
76 | }
77 | }
78 | }
79 | }
--------------------------------------------------------------------------------
/boxer/libs/google-play-services.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LarsWerkman/Boxer/454ca23371952d387fe5eb82b4dfa33b6326e45a/boxer/libs/google-play-services.jar
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/Execution.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer;
17 |
18 | /**
19 | * Indicates when Annotated method should be called
20 | */
21 | public enum Execution {
22 | BEFORE, AFTER
23 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/TypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer;
17 |
18 | /**
19 | * Serialize and deserialize Java objects.
20 | * {@link TypeAdapter} is a type-specific adapter.
21 | *
22 | * For example, a {@code TypeAdapter }
23 | * can serialize or deserialize {@code Date} instances.
24 | *
25 | * {@link com.larswerkman.boxer.annotations.Adapter} needs to be
26 | * placed on the {@link TypeAdapter} class to get the class to be
27 | * recognized by the annotation processor as a {@link TypeAdapter}.
28 | *
29 | * @param Type that needs to be serialized or deserialized.
30 | */
31 | public abstract class TypeAdapter {
32 |
33 | public TypeAdapter(){}
34 |
35 | /**
36 | * Serialize an object of type {@code T}
37 | * to a generic {@code Boxer>} object
38 | *
39 | * @param boxer The {@link Boxer} wrapper to serialize to
40 | * @param object the value to be serialized
41 | */
42 | public abstract void serialize(Boxer> boxer, T object);
43 |
44 | /**
45 | * Deserialize an object of type {@code T}
46 | * from a generic {@code Boxer>} object
47 | *
48 | * @param boxer The {@link Boxer} wrapper to deserialize from
49 | * @return deserialized instance of type {@code T}
50 | */
51 | public abstract T deserialize(Boxer> boxer);
52 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/annotations/Adapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.annotations;
17 |
18 | import java.lang.annotation.ElementType;
19 | import java.lang.annotation.Retention;
20 | import java.lang.annotation.RetentionPolicy;
21 | import java.lang.annotation.Target;
22 |
23 | /**
24 | * Annotation that defines that a {@link com.larswerkman.boxer.TypeAdapter} class
25 | * should be consumed by the annotation processor, to be registered as an
26 | * {@link com.larswerkman.boxer.TypeAdapter}.
27 | */
28 | @Target(ElementType.TYPE)
29 | @Retention(RetentionPolicy.SOURCE)
30 | public @interface Adapter {
31 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/annotations/Box.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.annotations;
17 |
18 | import java.lang.annotation.ElementType;
19 | import java.lang.annotation.Retention;
20 | import java.lang.annotation.RetentionPolicy;
21 | import java.lang.annotation.Target;
22 |
23 | /**
24 | * Class annotated with the {@link Box} annotation will have a {@link com.larswerkman.boxer.TypeAdapter}
25 | * generated for them.
26 | **/
27 | @Target(ElementType.TYPE)
28 | @Retention(RetentionPolicy.SOURCE)
29 | public @interface Box {
30 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/annotations/Deserialize.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.annotations;
17 |
18 | import com.larswerkman.boxer.Execution;
19 |
20 | import java.lang.annotation.ElementType;
21 | import java.lang.annotation.Retention;
22 | import java.lang.annotation.RetentionPolicy;
23 | import java.lang.annotation.Target;
24 |
25 | /**
26 | * Annotation that will be executed during deserialization of a
27 | * class annotated with an {@link Box} annotation.
28 | *
29 | * Methods annotated with the {@link Deserialize} annotation
30 | * have can have an argument of the type {@link com.larswerkman.boxer.Boxer}
31 | *
32 | * {@code @Deserialize public void deserialization(Boxer> boxer){}}
33 | * {@code @Deserialize public void deserialization(Boxer boxer){}}
34 | * {@code @Deserialize public void deserialization(){}}
35 | */
36 | @Target(ElementType.METHOD)
37 | @Retention(RetentionPolicy.SOURCE)
38 | public @interface Deserialize {
39 | /**
40 | * Determines when the execution of the method will be called.
41 | * {@code Execution.BEFORE} will be called before deserialization.
42 | * {@code Execution.AFTER} will be called after deserialization
43 | *
44 | * Default behaviour will be to be called after deserialization.
45 | *
46 | * @return Execution type
47 | */
48 | Execution value() default Execution.AFTER;
49 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/annotations/Serialize.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.annotations;
17 |
18 | import com.larswerkman.boxer.Execution;
19 |
20 | import java.lang.annotation.ElementType;
21 | import java.lang.annotation.Retention;
22 | import java.lang.annotation.RetentionPolicy;
23 | import java.lang.annotation.Target;
24 |
25 | /**
26 | * Annotation that will be executed during serialization of a
27 | * class annotated with an {@link Box} annotation.
28 | *
29 | * Methods annotated with the {@link Serialize} annotation
30 | * have can have an argument of the type {@link com.larswerkman.boxer.Boxer}
31 | *
32 | * {@code @Serialize public void serialization(Boxer> boxer){}}
33 | * {@code @Serialize public void serialization(Boxer boxer){}}
34 | * {@code @Serialize public void serialization(){}}
35 | */
36 | @Target(ElementType.METHOD)
37 | @Retention(RetentionPolicy.SOURCE)
38 | public @interface Serialize {
39 | /**
40 | * Determines when the execution of the method will be called.
41 | * {@code Execution.BEFORE} will be called before serialization.
42 | * {@code Execution.AFTER} will be called after serialization
43 | *
44 | * Default behaviour will be to be called after serialization.
45 | *
46 | * @return Execution type
47 | */
48 | Execution value() default Execution.AFTER;
49 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/annotations/Wrap.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.annotations;
17 |
18 | import java.lang.annotation.ElementType;
19 | import java.lang.annotation.Retention;
20 | import java.lang.annotation.RetentionPolicy;
21 | import java.lang.annotation.Target;
22 | import java.util.List;
23 |
24 | /**
25 | * Annotation that defines what type of Subclass a Superclass type field should be,
26 | * type of class should always have an empty constructor.
27 | */
28 | @Target(ElementType.FIELD)
29 | @Retention(RetentionPolicy.SOURCE)
30 | public @interface Wrap {
31 | Class value();
32 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/AdapterBinding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.squareup.javapoet.ClassName;
19 |
20 | import javax.lang.model.type.TypeMirror;
21 |
22 | /**
23 | * Created by lars on 05-06-15.
24 | */
25 | class AdapterBinding {
26 |
27 | private ClassName adapter;
28 | private TypeMirror type;
29 |
30 | public AdapterBinding(ClassName adapter, TypeMirror type){
31 | this.adapter = adapter;
32 | this.type = type;
33 | }
34 |
35 | public ClassName getAdapter() {
36 | return adapter;
37 | }
38 |
39 | public TypeMirror getType() {
40 | return type;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/AdaptersClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.larswerkman.boxer.TypeAdapter;
19 | import com.squareup.javapoet.*;
20 |
21 | import javax.annotation.Generated;
22 | import javax.lang.model.element.Modifier;
23 | import java.util.HashMap;
24 | import java.util.List;
25 |
26 | /**
27 | * Created by lars on 28-05-15.
28 | */
29 | final class AdaptersClass {
30 |
31 | private static final String HASHMAP_ADAPTERS_VARIABLE = "adapters";
32 | private static final String TYPE_VARIABLE = "type";
33 |
34 | private TypeName hashMapType = ParameterizedTypeName.get(HashMap.class, Class.class, TypeAdapter.class);
35 | private TypeVariableName T = TypeVariableName.get("T");
36 | private List adapters;
37 |
38 | public AdaptersClass(List adapters){
39 | this.adapters = adapters;
40 | }
41 |
42 | public TypeSpec build(){
43 | return TypeSpec.classBuilder(BoxerProcessor.ADAPTER_CLASS_NAME)
44 | .superclass(GeneratedAdapters.class)
45 | .addAnnotation(AnnotationSpec.builder(Generated.class)
46 | .addMember("value", "$S", BoxerProcessor.PROCESSOR_NAME)
47 | .build())
48 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
49 | .addField(hashMapType, HASHMAP_ADAPTERS_VARIABLE, Modifier.PRIVATE)
50 | .addMethod(constructor())
51 | .addMethod(getMethod())
52 | .build();
53 | }
54 |
55 | private MethodSpec constructor(){
56 | MethodSpec.Builder builder = MethodSpec.constructorBuilder()
57 | .addStatement("$N = new $T()", HASHMAP_ADAPTERS_VARIABLE, hashMapType);
58 | for(AdapterBinding adapter : adapters){
59 | builder.addStatement("$N.put($T.class, new $T())",
60 | HASHMAP_ADAPTERS_VARIABLE, TypeName.get(adapter.getType()), adapter.getAdapter());
61 | }
62 | return builder.build();
63 | }
64 |
65 | private MethodSpec getMethod(){
66 | return MethodSpec
67 | .methodBuilder(BoxerProcessor.ADAPTER_METHOD_GET)
68 | .addAnnotation(Override.class)
69 | .addModifiers(Modifier.PUBLIC)
70 | .addTypeVariable(T)
71 | .returns(ParameterizedTypeName.get(ClassName.get(TypeAdapter.class), T))
72 | .addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), T), TYPE_VARIABLE)
73 | .addStatement("return $N.get($N)", HASHMAP_ADAPTERS_VARIABLE, TYPE_VARIABLE)
74 | .build();
75 | }
76 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/ArrayFieldBinding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import javax.lang.model.type.TypeMirror;
19 |
20 | /**
21 | * Created by lars on 29-05-15.
22 | */
23 | class ArrayFieldBinding extends FieldBinding {
24 |
25 | public ArrayFieldBinding(String name, TypeMirror type, String method, boolean isPrivate) {
26 | super(name, type, method, isPrivate);
27 | }
28 |
29 | @Override
30 | public String method() {
31 | return super.method() + "Array";
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/BoxClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.larswerkman.boxer.Boxer;
19 | import com.larswerkman.boxer.Execution;
20 | import com.larswerkman.boxer.TypeAdapter;
21 | import com.squareup.javapoet.*;
22 |
23 | import javax.annotation.Generated;
24 | import javax.lang.model.element.Modifier;
25 | import java.util.List;
26 |
27 | /**
28 | * Created by lars on 27-05-15.
29 | */
30 | final class BoxClass {
31 |
32 | private static final String BOXER_VARIABLE = "boxer";
33 | private static final String BOXABLE_VARIABLE = "object";
34 | private static final ParameterizedTypeName BOXER_CLASS = ParameterizedTypeName.get(
35 | ClassName.get(Boxer.class),
36 | WildcardTypeName.subtypeOf(Object.class)
37 | );
38 |
39 | private String className;
40 | private ClassName targetClass;
41 |
42 | private List fields;
43 | private List methods;
44 |
45 | public BoxClass(String className, ClassName targetClass, List fields, List methods){
46 | this.className = className;
47 | this.targetClass = targetClass;
48 | this.fields = fields;
49 | this.methods = methods;
50 | }
51 |
52 | public TypeSpec build(){
53 | return TypeSpec.classBuilder(className)
54 | .superclass(ParameterizedTypeName.get(
55 | ClassName.get(TypeAdapter.class), targetClass))
56 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
57 | .addAnnotation(AnnotationSpec.builder(Generated.class)
58 | .addMember("value", "$S", BoxerProcessor.PROCESSOR_NAME)
59 | .build())
60 | .addMethod(serializeMethod())
61 | .addMethod(deserializeMethod())
62 | .build();
63 | }
64 |
65 | private MethodSpec serializeMethod() {
66 | MethodSpec.Builder builder = MethodSpec
67 | .methodBuilder(BoxerProcessor.METHOD_SERIALIZE)
68 | .addAnnotation(Override.class)
69 | .addModifiers(Modifier.PUBLIC)
70 | .addParameter(BOXER_CLASS, BOXER_VARIABLE)
71 | .addParameter(targetClass, BOXABLE_VARIABLE);
72 | for(MethodBinding method : methods){
73 | if(method.getMethod() == MethodBinding.Method.SERIALIZE
74 | && method.getExecution() == Execution.BEFORE){
75 | builder.addCode(method.brew(BOXABLE_VARIABLE, BOXER_VARIABLE));
76 | }
77 | }
78 | for(FieldBinding field : fields){
79 | builder.addCode(field.serialize(BOXER_VARIABLE, BOXABLE_VARIABLE));
80 | }
81 | for(MethodBinding method : methods){
82 | if(method.getMethod() == MethodBinding.Method.SERIALIZE
83 | && method.getExecution() == Execution.AFTER){
84 | builder.addCode(method.brew(BOXABLE_VARIABLE, BOXER_VARIABLE));
85 | }
86 | }
87 | return builder.build();
88 | }
89 |
90 | private MethodSpec deserializeMethod() {
91 | MethodSpec.Builder builder = MethodSpec
92 | .methodBuilder(BoxerProcessor.METHOD_DESERIALIZE)
93 | .addAnnotation(Override.class)
94 | .addModifiers(Modifier.PUBLIC)
95 | .returns(targetClass)
96 | .addParameter(BOXER_CLASS, BOXER_VARIABLE)
97 | .addStatement("$T $N = new $T()", targetClass, BOXABLE_VARIABLE, targetClass);
98 | for(MethodBinding method : methods){
99 | if(method.getMethod() == MethodBinding.Method.DESERIALIZE
100 | && method.getExecution() == Execution.BEFORE){
101 | builder.addCode(method.brew(BOXABLE_VARIABLE, BOXER_VARIABLE));
102 | }
103 | }
104 | for(FieldBinding field : fields){
105 | builder.addCode(field.deserialize(BOXER_VARIABLE, BOXABLE_VARIABLE));
106 | }
107 | for(MethodBinding method : methods){
108 | if(method.getMethod() == MethodBinding.Method.DESERIALIZE
109 | && method.getExecution() == Execution.AFTER){
110 | builder.addCode(method.brew(BOXABLE_VARIABLE, BOXER_VARIABLE));
111 | }
112 | }
113 | builder.addStatement("return $N", BOXABLE_VARIABLE);
114 | return builder.build();
115 | }
116 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/BoxerProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.larswerkman.boxer.annotations.*;
19 | import com.squareup.javapoet.ClassName;
20 | import com.squareup.javapoet.JavaFile;
21 |
22 | import javax.annotation.processing.*;
23 | import javax.lang.model.SourceVersion;
24 | import javax.lang.model.element.*;
25 | import javax.lang.model.type.*;
26 | import javax.lang.model.util.ElementFilter;
27 | import javax.lang.model.util.Elements;
28 | import javax.lang.model.util.Types;
29 | import javax.tools.Diagnostic;
30 | import java.io.IOException;
31 | import java.lang.annotation.Annotation;
32 | import java.util.*;
33 | import java.util.List;
34 |
35 | /**
36 | * Annotation Processor for processing the {@link com.larswerkman.boxer.annotations.Box} annotation
37 | */
38 | @SupportedAnnotationTypes({
39 | "com.larswerkman.boxer.annotations.Box",
40 | "com.larswerkman.boxer.annotations.Adapter"
41 | })
42 | public class BoxerProcessor extends AbstractProcessor {
43 |
44 | public static final String PROCESSOR_NAME = "com.larswerkman.boxer.internal.BoxerProcessor";
45 |
46 | public static final String ADAPTER_PACKAGE_NAME = "com.larswerkman.boxer";
47 | public static final String ADAPTER_CLASS_NAME = "GeneratedAdapters$$Boxer";
48 | public static final String ADAPTER_METHOD_GET = "getAdapter";
49 |
50 | public static final String CLASS_EXTENSION = "_TypeAdapter";
51 | public static final String METHOD_SERIALIZE = "serialize";
52 | public static final String METHOD_DESERIALIZE = "deserialize";
53 |
54 | private static TypeMirror TYPE_STRING;
55 | private static TypeMirror TYPE_LIST;
56 | private static TypeMirror TYPE_OBJECT;
57 | private static TypeMirror TYPE_ADAPTER;
58 | private static TypeMirror TYPE_BOXER;
59 | private static TypeMirror TYPE_BOXER_WILDCARD;
60 |
61 | private List adapters = new ArrayList();
62 |
63 | private Elements elementUtils;
64 | private Types typeUtils;
65 |
66 | private Messager log;
67 | private Filer filer;
68 |
69 | @Override
70 | public boolean process(Set extends TypeElement> annotations, RoundEnvironment env) {
71 | log = processingEnv.getMessager();
72 | filer = processingEnv.getFiler();
73 |
74 | elementUtils = processingEnv.getElementUtils();
75 | typeUtils = processingEnv.getTypeUtils();
76 |
77 | TYPE_OBJECT = elementUtils.getTypeElement("java.lang.Object").asType();
78 | TYPE_STRING = elementUtils.getTypeElement("java.lang.String").asType();
79 | TYPE_LIST = typeUtils.getDeclaredType(
80 | elementUtils.getTypeElement("java.util.List"),
81 | typeUtils.getWildcardType(TYPE_OBJECT, null));
82 | TYPE_ADAPTER = elementUtils.getTypeElement("com.larswerkman.boxer.TypeAdapter").asType();
83 | TYPE_BOXER = ((DeclaredType) elementUtils.getTypeElement("com.larswerkman.boxer.Boxer").asType())
84 | .asElement().asType();
85 | TYPE_BOXER_WILDCARD = typeUtils.getDeclaredType(
86 | elementUtils.getTypeElement("com.larswerkman.boxer.Boxer"),
87 | typeUtils.getWildcardType(TYPE_OBJECT, null)
88 | );
89 |
90 | Set extends Element> boxableElements = env.getElementsAnnotatedWith(Box.class);
91 | Set extends Element> adapterElements = env.getElementsAnnotatedWith(Adapter.class);
92 |
93 | //Process all adapters classes
94 | if(!adapterElements.isEmpty()) {
95 | adapters.addAll(parseTypeAdapters(adapterElements));
96 | }
97 |
98 | //Process all boxable classes
99 | for (Element element : boxableElements) {
100 | TypeElement typeElement = (TypeElement) element;
101 | if(!hasEmptyConstructor(typeElement)){
102 | log.printMessage(Diagnostic.Kind.ERROR,
103 | String.format("%s class must contain a non-args constructor",
104 | typeElement.getSimpleName()), typeElement);
105 | }
106 |
107 | List methodBindings = parseMethodAnnotations(typeElement);
108 | List bindings = parseBoxableFields(typeElement);
109 |
110 | String name = getCanonicalName(typeElement) + CLASS_EXTENSION;
111 | BoxClass boxClass = new BoxClass(name,
112 | ClassName.get(typeElement), bindings, methodBindings);
113 |
114 | JavaFile file = JavaFile.builder(getPackage(typeElement), boxClass.build()).build();
115 | try {
116 | file.writeTo(filer);
117 | adapters.add(new AdapterBinding(ClassName.get(file.packageName, name),typeElement.asType()));
118 | } catch (IOException e) {
119 | log.printMessage(Diagnostic.Kind.ERROR, e.getMessage(), element);
120 | }
121 | }
122 |
123 | if(!adapters.isEmpty()) {
124 | JavaFile adaptersClass = JavaFile.builder(ADAPTER_PACKAGE_NAME,
125 | new AdaptersClass(adapters).build()).build();
126 | try {
127 | adaptersClass.writeTo(filer);
128 | adapters.clear();
129 | } catch (IOException e) {
130 | log.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
131 | }
132 | }
133 |
134 | return true;
135 | }
136 |
137 | private List parseTypeAdapters(Set extends Element> elements){
138 | List adapters = new ArrayList();
139 | for(Element element : elements){
140 | TypeElement typeElement = (TypeElement) element;
141 | TypeMirror superType = typeElement.getSuperclass();
142 |
143 | if(!hasEmptyConstructor(typeElement)){
144 | log.printMessage(Diagnostic.Kind.ERROR,
145 | String.format("%s class must contain a non-args constructor",
146 | typeElement.getSimpleName()), typeElement);
147 | }
148 |
149 | //Check for superclass without A WildcardType
150 | Element superElement = typeUtils.asElement(superType);
151 | if(typeUtils.isSameType(superElement.asType(), TYPE_ADAPTER)){
152 |
153 | //Returns first TypeArgument which will be the target class.
154 | TypeMirror targetType = ((DeclaredType) superType).getTypeArguments().get(0);
155 | adapters.add(new AdapterBinding(ClassName.get(typeElement), targetType));
156 | } else {
157 | log.printMessage(Diagnostic.Kind.ERROR,
158 | String.format("%s class must extend TypeAdapter class",
159 | typeElement.getSimpleName()), typeElement);
160 | }
161 | }
162 | return adapters;
163 | }
164 |
165 | private List parseMethodAnnotations(TypeElement typeElement){
166 | List bindings = new ArrayList();
167 | for(Element element : getAllElements(typeElement)){
168 |
169 | if(!element.getKind().equals(ElementKind.METHOD)
170 | || !hasAcceptableMethodAnnotation(element)){
171 | continue;
172 | }
173 |
174 | if(element.getModifiers().contains(Modifier.PRIVATE)){
175 | log.printMessage(Diagnostic.Kind.ERROR,
176 | String.format("Annotated method %s should be accessible",
177 | element.getSimpleName()), typeElement);
178 | }
179 |
180 | ExecutableElement method = (ExecutableElement) element;
181 | boolean hasArgument = false;
182 |
183 | List extends VariableElement> params = method.getParameters();
184 | if(params.size() == 1){
185 | if(isBoxer(params.get(0).asType())){
186 | hasArgument = true;
187 | } else {
188 | log.printMessage(Diagnostic.Kind.WARNING, params.get(0).asType().toString());
189 | log.printMessage(Diagnostic.Kind.WARNING, TYPE_BOXER.toString());
190 | log.printMessage(Diagnostic.Kind.WARNING, TYPE_BOXER_WILDCARD.toString());
191 | log.printMessage(Diagnostic.Kind.ERROR,
192 | String.format("Annotated method %s argument must be of type Boxer",
193 | element.getSimpleName()), typeElement);
194 | }
195 | } else if(params.size() > 1) {
196 | log.printMessage(Diagnostic.Kind.ERROR,
197 | String.format("Annotated method %s can't have more than 1 argument",
198 | element.getSimpleName()), typeElement);
199 | }
200 |
201 | Serialize serialize = element.getAnnotation(Serialize.class);
202 | if(serialize != null){
203 | bindings.add(new MethodBinding(element.getSimpleName().toString(),
204 | MethodBinding.Method.SERIALIZE, serialize.value(), hasArgument));
205 | }
206 |
207 | Deserialize deserialize = element.getAnnotation(Deserialize.class);
208 | if(deserialize != null){
209 | bindings.add(new MethodBinding(element.getSimpleName().toString(),
210 | MethodBinding.Method.DESERIALIZE, deserialize.value(), hasArgument));
211 | }
212 | }
213 | return bindings;
214 | }
215 |
216 | private List parseBoxableFields(TypeElement typeElement){
217 | List bindings = new ArrayList();
218 | for(Element field : getAllElements(typeElement)){
219 |
220 | //Check if the element isn't a of the type field or has a transient modifier
221 | if(!field.getKind().isField() || field.getModifiers().contains(Modifier.TRANSIENT)){
222 | continue;
223 | }
224 |
225 | //Check for a Final modifier.
226 | if(field.getModifiers().contains(Modifier.FINAL)){
227 | log.printMessage(Diagnostic.Kind.ERROR,
228 | String.format("%s field can't be final. " +
229 | "Consider making it transient or remove the final modifier",
230 | field.getSimpleName()), typeElement);
231 | }
232 |
233 | //Check for a Private modifier then check if it doesn't contain default getters and setters.
234 | boolean isPrivate = false;
235 | if(field.getModifiers().contains(Modifier.PRIVATE) &&
236 | !hasGetterAndSetter(getAllElements(typeElement), field)){
237 | log.printMessage(Diagnostic.Kind.ERROR,
238 | String.format("%s field is private, " +
239 | "and doesn't have a suitable getter and setter",
240 | field.getSimpleName()), typeElement);
241 | } else if(field.getModifiers().contains(Modifier.PRIVATE)){
242 | isPrivate = true;
243 | }
244 |
245 | //Check if the a wrap annotation exists and is assignable from the class.
246 | TypeMirror wrapType = getWrapAnnotationType(field.getAnnotation(Wrap.class));
247 | if (wrapType != null && !typeUtils.isAssignable(wrapType, field.asType())) {
248 | log.printMessage(Diagnostic.Kind.ERROR,
249 | String.format("%s field can't be annotated with a @Wrap annotation " +
250 | "of a not assignable class of type %s",
251 | field.getSimpleName(), wrapType.toString()), typeElement);
252 | }
253 |
254 | if(!isAcceptable(getType(field.asType()))){
255 | log.printMessage(Diagnostic.Kind.ERROR,
256 | String.format("%s field can't be of type %s",
257 | field.getSimpleName().toString(), field.asType().toString()));
258 | }
259 |
260 | //Check for appropriate field and add the appropriate binding.
261 | String methodType = getMethodType(getType(field.asType()));
262 | if(isArray(field.asType())){
263 | bindings.add(new ArrayFieldBinding(field.getSimpleName().toString(),
264 | getStoreType(getType(field.asType())), methodType , isPrivate));
265 | } else if(isList(field.asType())){
266 | //Get the type of the list. if it has a Wrap annotation override it.
267 | TypeMirror listType = getListType(field.asType(), getType(field.asType()));
268 | if(wrapType != null) {
269 | listType = wrapType;
270 | }
271 | ClassName listTypeName = ClassName.get((TypeElement) ((DeclaredType) listType).asElement());
272 |
273 | bindings.add(new ListFieldBinding(field.getSimpleName().toString(),
274 | getStoreType(getType(field.asType())), methodType, listTypeName, isPrivate));
275 | } else {
276 | bindings.add(new FieldBinding(field.getSimpleName().toString(),
277 | getStoreType(getType(field.asType())), methodType, isPrivate));
278 | }
279 | }
280 | return bindings;
281 | }
282 |
283 | private TypeMirror getWrapAnnotationType(Wrap annotation) {
284 | if(annotation != null) {
285 | try {
286 | annotation.value();
287 | } catch (MirroredTypeException mte) {
288 | return mte.getTypeMirror();
289 | }
290 | }
291 | return null;
292 | }
293 |
294 | /**
295 | * Check if a Field has a getter and setter method.
296 | *
297 | * @param elements all elements to search through
298 | * @param field the current field element we want to check for getters and setters
299 | *
300 | * @return true if it has a correct getter and setter method, else false.
301 | */
302 | private boolean hasGetterAndSetter(List extends Element> elements, Element field){
303 | String fieldName = field.getSimpleName().toString();
304 | boolean getter = false;
305 | boolean setter = false;
306 |
307 | List methods = ElementFilter.methodsIn(elements);
308 | for (ExecutableElement method : methods) {
309 | //Checks if it has a get + fieldname method with 0 paramters which returns the same type as the field.
310 | if (method.getSimpleName().contentEquals(String.format("get%s", capitalize(fieldName)))
311 | && method.getParameters().size() == 0
312 | && typeUtils.isAssignable(method.getReturnType(), field.asType())) {
313 | getter = true;
314 | continue;
315 | }
316 |
317 | //Checks if it has a set + fieldname method with 1 parameter which accepts the same type as the field is.
318 | if (method.getSimpleName().contentEquals(String.format("set%s", capitalize(fieldName)))
319 | && method.getParameters().size() == 1
320 | && typeUtils.isAssignable(method.getParameters().get(0).asType(), field.asType())) {
321 | setter = true;
322 | }
323 | }
324 | return getter && setter;
325 | }
326 |
327 | private String getMethodType(TypeMirror type){
328 | if(isString(type)){
329 | return "String";
330 | } else if(isEnum(type)) {
331 | return "Enum";
332 | } else if(isAdapter(type) || isBoxable(type)){
333 | return "";
334 | }
335 |
336 | //Its a primitive or wrapper and we should unbox it.
337 | return unboxed(type);
338 | }
339 |
340 | private TypeMirror getStoreType(TypeMirror type){
341 | if(isBoxable(type)){
342 | return type;
343 | } else if(isEnum(type)){
344 | return type;
345 | } else if(isAdapter(type)){
346 | return type;
347 | }
348 | return null;
349 | }
350 |
351 | private boolean hasAcceptableMethodAnnotation(Element element){
352 | return (element.getAnnotation(Serialize.class) != null) ||
353 | (element.getAnnotation(Deserialize.class) != null);
354 | }
355 |
356 | private boolean isAcceptable(TypeMirror type) {
357 | return (isPrimitiveOrWrapper(type)
358 | || isString(type)
359 | || isBoxable(type)
360 | || isEnum(type)
361 | || isAdapter(type));
362 | }
363 |
364 | private boolean isAdapter(TypeMirror type){
365 | for(AdapterBinding adapter : adapters){
366 | if(typeUtils.isSameType(type, adapter.getType())){
367 | return true;
368 | }
369 | }
370 | return false;
371 | }
372 |
373 | private boolean isBoxer(TypeMirror type){
374 | return typeUtils.isAssignable(type, TYPE_BOXER)
375 | || typeUtils.isSameType(type, TYPE_BOXER_WILDCARD);
376 | }
377 |
378 | private boolean hasEmptyConstructor(TypeElement element){
379 | List constructors = ElementFilter
380 | .constructorsIn(element.getEnclosedElements());
381 | if(constructors.isEmpty()){
382 | return true;
383 | }
384 | for(ExecutableElement constructor : constructors){
385 | if(constructor.getParameters().isEmpty()){
386 | return true;
387 | }
388 | }
389 | return false;
390 | }
391 |
392 | private String unboxed(TypeMirror type) {
393 | if (type.getKind().isPrimitive()) {
394 | return capitalize(type.toString());
395 | }
396 | return capitalize(typeUtils.unboxedType(type).toString());
397 | }
398 |
399 | private boolean isPrimitiveOrWrapper(TypeMirror type) {
400 | return (type.getKind().isPrimitive()
401 | || type.toString().equals("java.lang.Byte")
402 | || type.toString().equals("java.lang.Short")
403 | || type.toString().equals("java.lang.Integer")
404 | || type.toString().equals("java.lang.Long")
405 | || type.toString().equals("java.lang.Float")
406 | || type.toString().equals("java.lang.Double")
407 | || type.toString().equals("java.lang.Character")
408 | || type.toString().equals("java.lang.Boolean"));
409 | }
410 |
411 | private boolean isString(TypeMirror type) {
412 | return typeUtils.isSameType(type, TYPE_STRING);
413 | }
414 |
415 | private boolean isBoxable(TypeMirror type){
416 | return typeUtils.asElement(type) != null && findAnnotationMirror(typeUtils.asElement(type), Box.class) != null;
417 | }
418 |
419 | private boolean isEnum(TypeMirror type) {
420 | Element element = typeUtils.asElement(type);
421 | return element != null && element
422 | .getKind() == ElementKind.ENUM;
423 | }
424 |
425 | private boolean isArray(TypeMirror type) {
426 | return type.getKind() == TypeKind.ARRAY;
427 | }
428 |
429 | private boolean isList(TypeMirror type){
430 | return typeUtils.isAssignable(type, TYPE_LIST);
431 | }
432 |
433 | private TypeMirror getType(TypeMirror type){
434 | if(isArray(type)){
435 | return ((ArrayType) type).getComponentType();
436 | } else if(isList(type)){
437 | return ((DeclaredType) type).getTypeArguments().get(0);
438 | }
439 | return type;
440 | }
441 |
442 | private TypeMirror getListType(TypeMirror type, TypeMirror arrayType){
443 | TypeMirror declaredListType = typeUtils.getDeclaredType(
444 | elementUtils.getTypeElement("java.util.List"), arrayType);
445 |
446 | //Check if the list type is the abstract List and make it an default ArrayList
447 | if (typeUtils.isSameType(declaredListType, type)) {
448 | return elementUtils.getTypeElement("java.util.ArrayList").asType();
449 | }
450 | return type;
451 | }
452 |
453 | private String getPackage(TypeElement type) {
454 | PackageElement pkg = elementUtils.getPackageOf(type);
455 | if (!pkg.isUnnamed()) {
456 | return pkg.getQualifiedName().toString();
457 | } else {
458 | return "";
459 | }
460 | }
461 |
462 | private String getCanonicalName(TypeElement type){
463 | return type.getQualifiedName().toString()
464 | .replace(getPackage(type) + ".", "")
465 | .replace(".", "$");
466 | }
467 |
468 | private List extends Element> getAllElements(TypeElement element){
469 | List fieldElements = new ArrayList(element.getEnclosedElements());
470 |
471 | TypeMirror superType = element.getSuperclass();
472 | while(!(superType instanceof NoType)){
473 | TypeElement typeElement = elementUtils.getTypeElement(superType.toString());
474 | fieldElements.addAll(typeElement.getEnclosedElements());
475 | superType = typeElement.getSuperclass();
476 | }
477 | return fieldElements;
478 | }
479 |
480 | public AnnotationMirror findAnnotationMirror(Element annotatedElement, Class extends Annotation> annotationClass) {
481 | List extends AnnotationMirror> annotationMirrors = annotatedElement.getAnnotationMirrors();
482 |
483 | for (AnnotationMirror annotationMirror : annotationMirrors) {
484 | TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType().asElement();
485 | if (isAnnotation(annotationElement, annotationClass)) {
486 | return annotationMirror;
487 | }
488 | }
489 | return null;
490 | }
491 |
492 | private boolean isAnnotation(TypeElement annotation, Class extends Annotation> annotationClass) {
493 | return annotation.getQualifiedName().toString().equals(annotationClass.getName());
494 | }
495 |
496 | public static String capitalize(String string) {
497 | return string.substring(0, 1).toUpperCase() + string.substring(1);
498 | }
499 |
500 | @Override
501 | public SourceVersion getSupportedSourceVersion() {
502 | return SourceVersion.latestSupported();
503 | }
504 | }
505 |
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/FieldBinding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.squareup.javapoet.CodeBlock;
19 |
20 | import javax.lang.model.type.TypeMirror;
21 |
22 | /**
23 | * Created by lars on 27-05-15.
24 | */
25 | class FieldBinding {
26 |
27 | String name;
28 | String method;
29 | TypeMirror type;
30 | boolean isPrivate;
31 |
32 | public FieldBinding(String name, TypeMirror type, String method, boolean isPrivate){
33 | this.name = name;
34 | this.method = method;
35 | this.type = type;
36 | this.isPrivate = isPrivate;
37 | }
38 |
39 | public String method(){
40 | return method;
41 | }
42 |
43 | public CodeBlock serialize(String boxer, String boxable){
44 | CodeBlock.Builder builder = CodeBlock.builder();
45 | if(isPrivate){
46 | builder.addStatement("$N.add$N($S, $N.get$N())",
47 | boxer, method(), name, boxable, BoxerProcessor.capitalize(name));
48 | } else {
49 | builder.addStatement("$N.add$N($S, $N.$N)",
50 | boxer, method(), name, boxable, name);
51 | }
52 | return builder.build();
53 | }
54 |
55 | public CodeBlock deserialize(String boxer, String boxable){
56 | CodeBlock.Builder builder = CodeBlock.builder();
57 | if(isPrivate){
58 | if(type == null){
59 | builder.addStatement("$N.set$N($N.get$N($S))",
60 | boxable, BoxerProcessor.capitalize(name), boxer, method(), name);
61 | } else {
62 | builder.addStatement("$N.set$N($N.get$N($S, $T.class))",
63 | boxable, BoxerProcessor.capitalize(name), boxer, method(), name, type);
64 | }
65 | } else {
66 | if (type == null) {
67 | builder.addStatement("$N.$N = $N.get$N($S)",
68 | boxable, name, boxer, method(), name);
69 | } else {
70 | builder.addStatement("$N.$N = $N.get$N($S, $T.class)",
71 | boxable, name, boxer, method(), name, type);
72 | }
73 | }
74 | return builder.build();
75 | }
76 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/GeneratedAdapters.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.larswerkman.boxer.TypeAdapter;
19 |
20 | /**
21 | * Superclass for Auto-Generated class that returns the correct TypeAdapter.
22 | */
23 | public abstract class GeneratedAdapters {
24 |
25 | public GeneratedAdapters(){
26 |
27 | }
28 |
29 | public abstract TypeAdapter getAdapter(Class type);
30 | }
31 |
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/ListFieldBinding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.squareup.javapoet.CodeBlock;
19 | import com.squareup.javapoet.TypeName;
20 |
21 | import javax.lang.model.type.TypeMirror;
22 |
23 | /**
24 | * Created by lars on 29-05-15.
25 | */
26 | class ListFieldBinding extends FieldBinding {
27 |
28 | TypeName listType;
29 |
30 | public ListFieldBinding(String name, TypeMirror type, String method, TypeName listType, boolean isPrivate) {
31 | super(name, type, method , isPrivate);
32 | this.listType = listType;
33 | }
34 |
35 | @Override
36 | public String method() {
37 | return super.method() + "List";
38 | }
39 |
40 | @Override
41 | public CodeBlock deserialize(String boxer, String boxable) {
42 | CodeBlock.Builder builder = CodeBlock.builder();
43 | if(isPrivate){
44 | if(type == null){
45 | builder.addStatement("$N.set$N($N.get$N($S, $T.class))",
46 | boxable, BoxerProcessor.capitalize(name), boxer, method(), name, listType);
47 | } else {
48 | builder.addStatement("$N.set$N($N.get$N($S, $T.class, $T.class))",
49 | boxable, BoxerProcessor.capitalize(name), boxer, method(), name, type, listType);
50 | }
51 | } else {
52 | if (type == null) {
53 | builder.addStatement("$N.$N = $N.get$N($S, $T.class)",
54 | boxable, name, boxer, method(), name, listType);
55 | } else {
56 | builder.addStatement("$N.$N = $N.get$N($S, $T.class, $T.class)",
57 | boxable, name, boxer, method(), name, type, listType);
58 | }
59 | }
60 | return builder.build();
61 | }
62 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/internal/MethodBinding.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.internal;
17 |
18 | import com.larswerkman.boxer.Execution;
19 | import com.squareup.javapoet.CodeBlock;
20 |
21 | /**
22 | * Created by lars on 24-06-15.
23 | */
24 | class MethodBinding {
25 |
26 | public enum Method {
27 | SERIALIZE, DESERIALIZE
28 | }
29 |
30 | private String name;
31 | private Method method;
32 | private Execution execution;
33 | private boolean hasArgument;
34 |
35 | public MethodBinding(String name, Method method, Execution execution, boolean hasArgument){
36 | this.name = name;
37 | this.method = method;
38 | this.execution = execution;
39 | this.hasArgument = hasArgument;
40 | }
41 |
42 | public Method getMethod() {
43 | return method;
44 | }
45 |
46 | public Execution getExecution() {
47 | return execution;
48 | }
49 |
50 | public CodeBlock brew(String boxable, String boxer){
51 | CodeBlock.Builder builder = CodeBlock.builder();
52 | if(hasArgument){
53 | builder.addStatement("$N.$N($N)", boxable, name, boxer);
54 | } else {
55 | builder.addStatement("$N.$N()", boxable, name);
56 | }
57 | return builder.build();
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/wrappers/android/BundleWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.wrappers.android;
17 |
18 | import android.os.Bundle;
19 | import com.larswerkman.boxer.Boxer;
20 |
21 | import java.lang.reflect.Array;
22 | import java.lang.reflect.Method;
23 | import java.util.List;
24 |
25 | /**
26 | * BundleWrapper defines how the Boxer class should write to a {@link android.os.Bundle}
27 | */
28 | public class BundleWrapper extends Boxer {
29 |
30 | public Bundle bundle;
31 |
32 | public BundleWrapper(Bundle object) {
33 | super(object);
34 | this.bundle = object;
35 | }
36 |
37 | @Override
38 | public void add(String key, Object value) {
39 | this.bundle.putBundle(key, serialize(new BundleWrapper(new Bundle()), value));
40 | }
41 |
42 | @Override
43 | public void addArray(String key, Object[] value) {
44 | Bundle bundle = new Bundle();
45 | bundle.putInt("size", value.length);
46 | for(int i = 0; i < value.length; i++){
47 | bundle.putBundle(String.valueOf(i), serialize(new BundleWrapper(new Bundle()), value[i]));
48 | }
49 | this.bundle.putBundle(key, bundle);
50 | }
51 |
52 | @Override
53 | public void addList(String key, List> value) {
54 | Bundle bundle = new Bundle();
55 | bundle.putInt("size", value.size());
56 | for(int i = 0; i < value.size(); i++){
57 | bundle.putBundle(String.valueOf(i), serialize(new BundleWrapper(new Bundle()), value.get(i)));
58 | }
59 | this.bundle.putBundle(key, bundle);
60 | }
61 |
62 | @Override
63 | public void addEnum(String key, Enum value) {
64 | this.bundle.putString(key, value.name());
65 | }
66 |
67 | @Override
68 | public void addEnumArray(String key, Enum[] value) {
69 | String[] strings = new String[value.length];
70 | for(int i = 0; i < strings.length; i++){
71 | strings[i] = value[i].name();
72 | }
73 | this.bundle.putStringArray(key, strings);
74 | }
75 |
76 | @Override
77 | public void addEnumList(String key, List extends Enum> value) {
78 | String[] strings = new String[value.size()];
79 | for(int i = 0; i < value.size(); i++){
80 | strings[i] = value.get(i).name();
81 | }
82 | this.bundle.putStringArray(key, strings);
83 | }
84 |
85 | @Override
86 | public void addString(String key, String value) {
87 | this.bundle.putString(key, value);
88 | }
89 |
90 | @Override
91 | public void addStringArray(String key, String[] value) {
92 | this.bundle.putStringArray(key, value);
93 | }
94 |
95 | @Override
96 | public void addStringList(String key, List value) {
97 | this.bundle.putStringArray(key, (String[]) value.toArray());
98 | }
99 |
100 | @Override
101 | public void addBoolean(String key, boolean value) {
102 | this.bundle.putBoolean(key, value);
103 | }
104 |
105 | @Override
106 | public void addBooleanArray(String key, boolean[] value) {
107 | this.bundle.putBooleanArray(key, value);
108 | }
109 |
110 | @Override
111 | public void addBooleanList(String key, List value) {
112 | boolean[] bools = new boolean[value.size()];
113 | for(int i = 0; i < value.size(); i++){
114 | bools[i] = value.get(i);
115 | }
116 | this.bundle.putBooleanArray(key, bools);
117 | }
118 |
119 | @Override
120 | public void addByte(String key, byte value) {
121 | this.bundle.putByte(key, value);
122 | }
123 |
124 | @Override
125 | public void addByteArray(String key, byte[] value) {
126 | this.bundle.putByteArray(key, value);
127 | }
128 |
129 | @Override
130 | public void addByteList(String key, List value) {
131 | byte[] bytes = new byte[value.size()];
132 | for(int i = 0; i < value.size(); i++){
133 | bytes[i] = value.get(i);
134 | }
135 | this.bundle.putByteArray(key, bytes);
136 | }
137 |
138 | @Override
139 | public void addChar(String key, char value) {
140 | this.bundle.putChar(key, value);
141 | }
142 |
143 | @Override
144 | public void addCharArray(String key, char[] value) {
145 | this.bundle.putCharArray(key, value);
146 | }
147 |
148 | @Override
149 | public void addCharList(String key, List value) {
150 | char[] chars = new char[value.size()];
151 | for(int i = 0; i < value.size(); i++){
152 | chars[i] = value.get(i);
153 | }
154 | this.bundle.putCharArray(key, chars);
155 | }
156 |
157 | @Override
158 | public void addShort(String key, short value) {
159 | this.bundle.putShort(key, value);
160 | }
161 |
162 | @Override
163 | public void addShortArray(String key, short[] value) {
164 | this.bundle.putShortArray(key, value);
165 | }
166 |
167 | @Override
168 | public void addShortList(String key, List value) {
169 | short[] shorts = new short[value.size()];
170 | for(int i = 0; i < value.size(); i++){
171 | shorts[i] = value.get(i);
172 | }
173 | this.bundle.putShortArray(key, shorts);
174 | }
175 |
176 | @Override
177 | public void addInt(String key, int value) {
178 | this.bundle.putInt(key, value);
179 | }
180 |
181 | @Override
182 | public void addIntArray(String key, int[] value) {
183 | this.bundle.putIntArray(key, value);
184 | }
185 |
186 | @Override
187 | public void addIntList(String key, List value) {
188 | int[] ints = new int[value.size()];
189 | for(int i = 0; i < value.size(); i++){
190 | ints[i] = value.get(i);
191 | }
192 | this.bundle.putIntArray(key, ints);
193 | }
194 |
195 | @Override
196 | public void addLong(String key, long value) {
197 | this.bundle.putLong(key, value);
198 | }
199 |
200 | @Override
201 | public void addLongArray(String key, long[] value) {
202 | this.bundle.putLongArray(key, value);
203 | }
204 |
205 | @Override
206 | public void addLongList(String key, List value) {
207 | long[] longs = new long[value.size()];
208 | for(int i = 0; i < value.size(); i++){
209 | longs[i] = value.get(i);
210 | }
211 | this.bundle.putLongArray(key, longs);
212 | }
213 |
214 | @Override
215 | public void addDouble(String key, double value) {
216 | this.bundle.putDouble(key, value);
217 | }
218 |
219 | @Override
220 | public void addDoubleArray(String key, double[] value) {
221 | this.bundle.putDoubleArray(key, value);
222 | }
223 |
224 | @Override
225 | public void addDoubleList(String key, List value) {
226 | double[] doubles = new double[value.size()];
227 | for(int i = 0; i < value.size(); i++){
228 | doubles[i] = value.get(i);
229 | }
230 | this.bundle.putDoubleArray(key, doubles);
231 | }
232 |
233 | @Override
234 | public void addFloat(String key, float value) {
235 | this.bundle.putFloat(key, value);
236 | }
237 |
238 | @Override
239 | public void addFloatArray(String key, float[] value) {
240 | this.bundle.putFloatArray(key, value);
241 | }
242 |
243 | @Override
244 | public void addFloatList(String key, List value) {
245 | float[] floats = new float[value.size()];
246 | for(int i = 0; i < value.size(); i++){
247 | floats[i] = value.get(i);
248 | }
249 | this.bundle.putFloatArray(key, floats);
250 | }
251 |
252 | @Override
253 | public T get(String key, Class clazz) {
254 | Bundle bundle = this.bundle.getBundle(key);
255 | if(bundle == null){
256 | return null;
257 | }
258 | return deserialize(new BundleWrapper(this.bundle.getBundle(key)), clazz);
259 | }
260 |
261 | @Override
262 | public T[] getArray(String key, Class clazz) {
263 | Bundle bundle = this.bundle.getBundle(key);
264 | if(bundle == null) {
265 | return null;
266 | }
267 |
268 | int size = bundle.getInt("size");
269 | T[] boxables = (T[]) Array.newInstance(clazz, size);
270 | for(int i = 0; i < size; i++){
271 | boxables[i] = deserialize(new BundleWrapper(bundle.getBundle(String.valueOf(i))), clazz);
272 | }
273 | return boxables;
274 | }
275 |
276 | @Override
277 | public > E getList(String key, Class clazz, Class listtype) {
278 | Bundle bundle = this.bundle.getBundle(key);
279 | if(bundle == null) {
280 | return null;
281 | }
282 |
283 | int size = bundle.getInt("size");
284 | E boxables = null;
285 | try {
286 | boxables = listtype.newInstance();
287 | for (int i = 0; i < size; i++) {
288 | boxables.add(deserialize(new BundleWrapper(bundle.getBundle(String.valueOf(i))), clazz));
289 | }
290 | } catch (Exception ignored){};
291 | return boxables;
292 | }
293 |
294 | private T retrieveEnum(String value, Class clazz){
295 | T en = null;
296 | try{
297 | Method method = clazz.getMethod("valueOf", String.class);
298 | en = (T) method.invoke(null, value);
299 | } catch (Exception e){}
300 | return en;
301 | }
302 |
303 | @Override
304 | public T getEnum(String key, Class clazz) {
305 | return retrieveEnum(this.bundle.getString(key), clazz);
306 | }
307 |
308 | @Override
309 | public T[] getEnumArray(String key, Class clazz) {
310 | String[] values = this.bundle.getStringArray(key);
311 | if(values == null) {
312 | return null;
313 | }
314 |
315 | T[] enums = (T[]) Array.newInstance(clazz, values.length);
316 | for(int i = 0; i < values.length; i++){
317 | enums[i] = retrieveEnum(values[i], clazz);
318 | }
319 | return enums;
320 | }
321 |
322 | @Override
323 | public > E getEnumList(String key, Class clazz, Class listtype) {
324 | String[] values = this.bundle.getStringArray(key);
325 | if(values == null) {
326 | return null;
327 | }
328 |
329 | E enums = null;
330 | try {
331 | enums = listtype.newInstance();
332 | for (int i = 0; i < values.length; i++) {
333 | enums.add(retrieveEnum(values[i], clazz));
334 | }
335 | } catch (Exception e){};
336 | return enums;
337 | }
338 |
339 | @Override
340 | public String getString(String key) {
341 | return this.bundle.getString(key);
342 | }
343 |
344 | @Override
345 | public String[] getStringArray(String key) {
346 | return this.bundle.getStringArray(key);
347 | }
348 |
349 | @Override
350 | public > T getStringList(String key, Class listtype) {
351 | String[] values = this.bundle.getStringArray(key);
352 | if(values == null) {
353 | return null;
354 | }
355 |
356 | T strings = null;
357 | try {
358 | strings = listtype.newInstance();
359 | for (int i = 0; i < values.length; i++) {
360 | strings.add(values[i]);
361 | }
362 | } catch (Exception e){};
363 | return strings;
364 | }
365 |
366 | @Override
367 | public boolean getBoolean(String key) {
368 | return this.bundle.getBoolean(key);
369 | }
370 |
371 | @Override
372 | public boolean[] getBooleanArray(String key) {
373 | return this.bundle.getBooleanArray(key);
374 | }
375 |
376 | @Override
377 | public > T getBooleanList(String key, Class listtype) {
378 | boolean[] values = this.bundle.getBooleanArray(key);
379 | if(values == null) {
380 | return null;
381 | }
382 |
383 | T booleans = null;
384 | try {
385 | booleans = listtype.newInstance();
386 | for (int i = 0; i < values.length; i++) {
387 | booleans.add(values[i]);
388 | }
389 | } catch (Exception e){};
390 | return booleans;
391 | }
392 |
393 | @Override
394 | public byte getByte(String key) {
395 | return this.bundle.getByte(key);
396 | }
397 |
398 | @Override
399 | public byte[] getByteArray(String key) {
400 | return this.bundle.getByteArray(key);
401 | }
402 |
403 | @Override
404 | public > T getByteList(String key, Class listtype) {
405 | byte[] values = this.bundle.getByteArray(key);
406 | if(values == null) {
407 | return null;
408 | }
409 |
410 | T bytes = null;
411 | try {
412 | bytes = listtype.newInstance();
413 | for (int i = 0; i < values.length; i++) {
414 | bytes.add(values[i]);
415 | }
416 | } catch (Exception e){};
417 | return bytes;
418 | }
419 |
420 | @Override
421 | public char getChar(String key) {
422 | return this.bundle.getChar(key);
423 | }
424 |
425 | @Override
426 | public char[] getCharArray(String key) {
427 | return this.bundle.getCharArray(key);
428 | }
429 |
430 | @Override
431 | public > T getCharList(String key, Class listtype) {
432 | char[] values = this.bundle.getCharArray(key);
433 | if(values == null) {
434 | return null;
435 | }
436 |
437 | T chars = null;
438 | try {
439 | chars = listtype.newInstance();
440 | for (int i = 0; i < values.length; i++) {
441 | chars.add(values[i]);
442 | }
443 | } catch (Exception e){};
444 | return chars;
445 | }
446 |
447 | @Override
448 | public short getShort(String key) {
449 | return this.bundle.getShort(key);
450 | }
451 |
452 | @Override
453 | public short[] getShortArray(String key) {
454 | return this.bundle.getShortArray(key);
455 | }
456 |
457 | @Override
458 | public > T getShortList(String key, Class listtype) {
459 | short[] values = this.bundle.getShortArray(key);
460 | if(values == null) {
461 | return null;
462 | }
463 |
464 | T shorts = null;
465 | try {
466 | shorts = listtype.newInstance();
467 | for (int i = 0; i < values.length; i++) {
468 | shorts.add(values[i]);
469 | }
470 | } catch (Exception e){};
471 | return shorts;
472 | }
473 |
474 | @Override
475 | public int getInt(String key) {
476 | return this.bundle.getInt(key);
477 | }
478 |
479 | @Override
480 | public int[] getIntArray(String key) {
481 | return this.bundle.getIntArray(key);
482 | }
483 |
484 | @Override
485 | public > T getIntList(String key, Class listtype) {
486 | int[] values = this.bundle.getIntArray(key);
487 | if(values == null) {
488 | return null;
489 | }
490 |
491 | T ints = null;
492 | try {
493 | ints = listtype.newInstance();
494 | for (int i = 0; i < values.length; i++) {
495 | ints.add(values[i]);
496 | }
497 | } catch (Exception e){};
498 | return ints;
499 | }
500 |
501 | @Override
502 | public long getLong(String key) {
503 | return this.bundle.getLong(key);
504 | }
505 |
506 | @Override
507 | public long[] getLongArray(String key) {
508 | return this.bundle.getLongArray(key);
509 | }
510 |
511 | @Override
512 | public > T getLongList(String key, Class listtype) {
513 | long[] values = this.bundle.getLongArray(key);
514 | if(values == null) {
515 | return null;
516 | }
517 |
518 | T longs = null;
519 | try {
520 | longs = listtype.newInstance();
521 | for (int i = 0; i < values.length; i++) {
522 | longs.add(values[i]);
523 | }
524 | } catch (Exception e){};
525 | return longs;
526 | }
527 |
528 | @Override
529 | public double getDouble(String key) {
530 | return this.bundle.getDouble(key);
531 | }
532 |
533 | @Override
534 | public double[] getDoubleArray(String key) {
535 | return this.bundle.getDoubleArray(key);
536 | }
537 |
538 | @Override
539 | public > T getDoubleList(String key, Class listtype) {
540 | double[] values = this.bundle.getDoubleArray(key);
541 | if(values == null) {
542 | return null;
543 | }
544 |
545 | T doubles = null;
546 | try {
547 | doubles = listtype.newInstance();
548 | for (int i = 0; i < values.length; i++) {
549 | doubles.add(values[i]);
550 | }
551 | } catch (Exception e){};
552 | return doubles;
553 | }
554 |
555 | @Override
556 | public float getFloat(String key) {
557 | return this.bundle.getFloat(key);
558 | }
559 |
560 | @Override
561 | public float[] getFloatArray(String key) {
562 | return this.bundle.getFloatArray(key);
563 | }
564 |
565 | @Override
566 | public > T getFloatList(String key, Class listtype) {
567 | float[] values = this.bundle.getFloatArray(key);
568 | if(values == null) {
569 | return null;
570 | }
571 |
572 | T floats = null;
573 | try {
574 | floats = listtype.newInstance();
575 | for (int i = 0; i < values.length; i++) {
576 | floats.add(values[i]);
577 | }
578 | } catch (Exception e){};
579 | return floats;
580 | }
581 | }
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/wrappers/android/DataMapWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.wrappers.android;
17 |
18 | import com.google.android.gms.wearable.DataMap;
19 | import com.larswerkman.boxer.Boxer;
20 |
21 | import java.lang.reflect.Array;
22 | import java.lang.reflect.Method;
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | /**
27 | * DataMapWrapper defines how the Boxer class should write to a {@link com.google.android.gms.wearable.DataMap}
28 | */
29 | public class DataMapWrapper extends Boxer {
30 |
31 | private DataMap dataMap;
32 |
33 | public DataMapWrapper(DataMap object) {
34 | super(object);
35 | this.dataMap = object;
36 | }
37 |
38 | @Override
39 | public void add(String key, Object value) {
40 | this.dataMap.putDataMap(key, serialize(new DataMapWrapper(new DataMap()), value));
41 | }
42 |
43 | @Override
44 | public void addArray(String key, Object[] value) {
45 | ArrayList dataMaps = new ArrayList();
46 | for(Object box : value){
47 | dataMaps.add(serialize(new DataMapWrapper(new DataMap()), box));
48 | }
49 | this.dataMap.putDataMapArrayList(key, dataMaps);
50 | }
51 |
52 | @Override
53 | public void addList(String key, List> value) {
54 | addArray(key, value.toArray());
55 | }
56 |
57 | @Override
58 | public void addEnum(String key, Enum value) {
59 | this.dataMap.putString(key, value.name());
60 | }
61 |
62 | @Override
63 | public void addEnumArray(String key, Enum[] value) {
64 | String[] strings = new String[value.length];
65 | for(int i = 0; i < strings.length; i++){
66 | strings[i] = value[i].name();
67 | }
68 | this.dataMap.putStringArray(key, strings);
69 | }
70 |
71 | @Override
72 | public void addEnumList(String key, List extends Enum> value) {
73 | String[] strings = new String[value.size()];
74 | for(int i = 0; i < value.size(); i++){
75 | strings[i] = value.get(i).name();
76 | }
77 | this.dataMap.putStringArray(key, strings);
78 | }
79 |
80 | @Override
81 | public void addString(String key, String value) {
82 | this.dataMap.putString(key, value);
83 | }
84 |
85 | @Override
86 | public void addStringArray(String key, String[] value) {
87 | this.dataMap.putStringArray(key, value);
88 | }
89 |
90 | @Override
91 | public void addStringList(String key, List value) {
92 | this.dataMap.putStringArray(key, (String[]) value.toArray());
93 | }
94 |
95 | @Override
96 | public void addBoolean(String key, boolean value) {
97 | this.dataMap.putBoolean(key, value);
98 | }
99 |
100 | @Override
101 | public void addBooleanArray(String key, boolean[] value) {
102 | byte[] bytes = new byte[value.length];
103 | for(int i = 0; i < value.length; i++){
104 | bytes[i] = value[i] ? Byte.MAX_VALUE : Byte.MIN_VALUE;
105 | }
106 | this.dataMap.putByteArray(key, bytes);
107 | }
108 |
109 | @Override
110 | public void addBooleanList(String key, List value) {
111 | byte[] bytes = new byte[value.size()];
112 | for(int i = 0; i < value.size(); i++){
113 | bytes[i] = value.get(i) ? Byte.MAX_VALUE : Byte.MIN_VALUE;
114 | }
115 | this.dataMap.putByteArray(key, bytes);
116 | }
117 |
118 | @Override
119 | public void addByte(String key, byte value) {
120 | this.dataMap.putByte(key, value);
121 | }
122 |
123 | @Override
124 | public void addByteArray(String key, byte[] value) {
125 | this.dataMap.putByteArray(key, value);
126 | }
127 |
128 | @Override
129 | public void addByteList(String key, List value) {
130 | byte[] bytes = new byte[value.size()];
131 | for(int i = 0; i < value.size(); i++){
132 | bytes[i] = value.get(i);
133 | }
134 | this.dataMap.putByteArray(key, bytes);
135 | }
136 |
137 | @Override
138 | public void addChar(String key, char value) {
139 | this.dataMap.putString(key, Character.toString(value));
140 | }
141 |
142 | @Override
143 | public void addCharArray(String key, char[] value) {
144 | this.dataMap.putString(key, String.valueOf(value));
145 | }
146 |
147 | @Override
148 | public void addCharList(String key, List value) {
149 | char[] chars = new char[value.size()];
150 | for(int i = 0; i < value.size(); i++){
151 | value.get(i).toString();
152 | chars[i] = value.get(i);
153 | }
154 | this.dataMap.putString(key, String.valueOf(chars));
155 | }
156 |
157 | @Override
158 | public void addShort(String key, short value) {
159 | this.dataMap.putInt(key, value);
160 | }
161 |
162 | @Override
163 | public void addShortArray(String key, short[] value) {
164 | long[] chars = new long[value.length];
165 | for(int i = 0; i < value.length; i++){
166 | chars[i] = value[i];
167 | }
168 | this.dataMap.putLongArray(key, chars);
169 | }
170 |
171 | @Override
172 | public void addShortList(String key, List value) {
173 | long[] chars = new long[value.size()];
174 | for(int i = 0; i < value.size(); i++){
175 | chars[i] = value.get(i);
176 | }
177 | this.dataMap.putLongArray(key, chars);
178 | }
179 |
180 | @Override
181 | public void addInt(String key, int value) {
182 | this.dataMap.putInt(key, value);
183 | }
184 |
185 | @Override
186 | public void addIntArray(String key, int[] value) {
187 | long[] chars = new long[value.length];
188 | for(int i = 0; i < value.length; i++){
189 | chars[i] = value[i];
190 | }
191 | this.dataMap.putLongArray(key, chars);
192 | }
193 |
194 | @Override
195 | public void addIntList(String key, List value) {
196 | long[] chars = new long[value.size()];
197 | for(int i = 0; i < value.size(); i++){
198 | chars[i] = value.get(i);
199 | }
200 | this.dataMap.putLongArray(key, chars);
201 | }
202 |
203 | @Override
204 | public void addLong(String key, long value) {
205 | this.dataMap.putLong(key, value);
206 | }
207 |
208 | @Override
209 | public void addLongArray(String key, long[] value) {
210 | this.dataMap.putLongArray(key, value);
211 | }
212 |
213 | @Override
214 | public void addLongList(String key, List value) {
215 | long[] longs = new long[value.size()];
216 | for(int i = 0; i < value.size(); i++){
217 | longs[i] = value.get(i);
218 | }
219 | this.dataMap.putLongArray(key, longs);
220 | }
221 |
222 | @Override
223 | public void addDouble(String key, double value) {
224 | this.dataMap.putDouble(key, value);
225 | }
226 |
227 | @Override
228 | public void addDoubleArray(String key, double[] value) {
229 | float[] floats = new float[value.length];
230 | for(int i = 0; i < value.length; i++){
231 | floats[i] = (float) value[i];
232 | }
233 | this.dataMap.putFloatArray(key, floats);
234 | }
235 |
236 | @Override
237 | public void addDoubleList(String key, List value) {
238 | float[] floats = new float[value.size()];
239 | for(int i = 0; i < value.size(); i++){
240 | floats[i] = value.get(i).floatValue();
241 | }
242 | this.dataMap.putFloatArray(key, floats);
243 | }
244 |
245 | @Override
246 | public void addFloat(String key, float value) {
247 | this.dataMap.putFloat(key, value);
248 | }
249 |
250 | @Override
251 | public void addFloatArray(String key, float[] value) {
252 | this.dataMap.putFloatArray(key, value);
253 | }
254 |
255 | @Override
256 | public void addFloatList(String key, List value) {
257 | float[] floats = new float[value.size()];
258 | for(int i = 0; i < value.size(); i++){
259 | floats[i] = value.get(i);
260 | }
261 | this.dataMap.putFloatArray(key, floats);
262 | }
263 |
264 | @Override
265 | public T get(String key, Class clazz) {
266 | DataMap dataMap = this.dataMap.getDataMap(key);
267 | if(dataMap == null){
268 | return null;
269 | }
270 | return deserialize(new DataMapWrapper(dataMap), clazz);
271 | }
272 |
273 | @Override
274 | public T[] getArray(String key, Class clazz) {
275 | ArrayList dataMaps = this.dataMap.getDataMapArrayList(key);
276 | if(dataMaps == null){
277 | return null;
278 | }
279 |
280 | T[] objects = (T[]) Array.newInstance(clazz, dataMaps.size());
281 | for(int i = 0; i < dataMaps.size(); i++){
282 | objects[i] = deserialize(new DataMapWrapper(dataMaps.get(i)), clazz);
283 | }
284 | return objects;
285 | }
286 |
287 | @Override
288 | public > E getList(String key, Class clazz, Class listtype) {
289 | ArrayList dataMaps = this.dataMap.getDataMapArrayList(key);
290 | if(dataMaps == null){
291 | return null;
292 | }
293 |
294 | E objects = null;
295 | try {
296 | objects = listtype.newInstance();
297 | for (DataMap dataMap : dataMaps) {
298 | objects.add(deserialize(new DataMapWrapper(dataMap), clazz));
299 | }
300 | } catch (Exception ignored){};
301 | return objects;
302 | }
303 |
304 | public T retrieveEnum(String value, Class clazz){
305 | T en = null;
306 | try{
307 | Method method = clazz.getMethod("valueOf", String.class);
308 | en = (T) method.invoke(null, value);
309 | } catch (Exception e){}
310 | return en;
311 | }
312 |
313 | @Override
314 | public T getEnum(String key, Class clazz) {
315 | return retrieveEnum(this.dataMap.getString(key), clazz);
316 | }
317 |
318 | @Override
319 | public T[] getEnumArray(String key, Class clazz) {
320 | String[] values = this.dataMap.getStringArray(key);
321 | if(values == null){
322 | return null;
323 | }
324 |
325 | T[] enums = (T[]) Array.newInstance(clazz, values.length);
326 | for(int i = 0; i < values.length; i++){
327 | enums[i] = retrieveEnum(values[i], clazz);
328 | }
329 | return enums;
330 | }
331 |
332 | @Override
333 | public > E getEnumList(String key, Class clazz, Class listtype) {
334 | String[] values = this.dataMap.getStringArray(key);
335 | if(values == null){
336 | return null;
337 | }
338 |
339 | E enums = null;
340 | try {
341 | enums = listtype.newInstance();
342 | for (int i = 0; i < values.length; i++) {
343 | enums.add(retrieveEnum(values[i], clazz));
344 | }
345 | } catch (Exception e){};
346 | return enums;
347 | }
348 |
349 | @Override
350 | public String getString(String key) {
351 | return this.dataMap.getString(key);
352 | }
353 |
354 | @Override
355 | public String[] getStringArray(String key) {
356 | return this.dataMap.getStringArray(key);
357 | }
358 |
359 | @Override
360 | public > T getStringList(String key, Class listtype) {
361 | String[] values = this.dataMap.getStringArray(key);
362 | if(values == null){
363 | return null;
364 | }
365 |
366 | T strings = null;
367 | try {
368 | strings = listtype.newInstance();
369 | for (int i = 0; i < values.length; i++) {
370 | strings.add(values[i]);
371 | }
372 | } catch (Exception e){};
373 | return strings;
374 | }
375 |
376 | @Override
377 | public boolean getBoolean(String key) {
378 | return this.dataMap.getBoolean(key);
379 | }
380 |
381 | @Override
382 | public boolean[] getBooleanArray(String key) {
383 | byte[] bytes = this.dataMap.getByteArray(key);
384 | if(bytes == null){
385 | return null;
386 | }
387 |
388 | boolean[] bools = new boolean[bytes.length];
389 | for(int i = 0; i < bytes.length; i++){
390 | bools[i] = bytes[i] > Byte.MIN_VALUE;
391 | }
392 | return bools;
393 | }
394 |
395 | @Override
396 | public > T getBooleanList(String key, Class listtype) {
397 | byte[] bytes= this.dataMap.getByteArray(key);
398 | if(bytes == null){
399 | return null;
400 | }
401 |
402 | T booleans = null;
403 | try {
404 | booleans = listtype.newInstance();
405 | for (int i = 0; i < bytes.length; i++) {
406 | booleans.add(bytes[i] > Byte.MIN_VALUE);
407 | }
408 | } catch (Exception e){};
409 | return booleans;
410 | }
411 |
412 | @Override
413 | public byte getByte(String key) {
414 | return this.dataMap.getByte(key);
415 | }
416 |
417 | @Override
418 | public byte[] getByteArray(String key) {
419 | return this.dataMap.getByteArray(key);
420 | }
421 |
422 | @Override
423 | public > T getByteList(String key, Class listtype) {
424 | byte[] values = this.dataMap.getByteArray(key);
425 | if(values == null){
426 | return null;
427 | }
428 |
429 | T bytes = null;
430 | try {
431 | bytes = listtype.newInstance();
432 | for (int i = 0; i < values.length; i++) {
433 | bytes.add(values[i]);
434 | }
435 | } catch (Exception e){};
436 | return bytes;
437 | }
438 |
439 | @Override
440 | public char getChar(String key) {
441 | String chars = this.dataMap.getString(key);
442 | if(chars == null){
443 | return 0;
444 | }
445 |
446 | return chars.charAt(0);
447 | }
448 |
449 | @Override
450 | public char[] getCharArray(String key) {
451 | String chars = this.dataMap.getString(key);
452 | if(chars == null){
453 | return null;
454 | }
455 |
456 | return chars.toCharArray();
457 | }
458 |
459 | @Override
460 | public > T getCharList(String key, Class listtype) {
461 | String value = this.dataMap.getString(key);
462 | if(value == null){
463 | return null;
464 | }
465 |
466 |
467 | T chars = null;
468 | try {
469 | char[] values = value.toCharArray();
470 | chars = listtype.newInstance();
471 | for (int i = 0; i < values.length; i++) {
472 | chars.add(values[i]);
473 | }
474 | } catch (Exception e){};
475 | return chars;
476 | }
477 |
478 | @Override
479 | public short getShort(String key) {
480 | return (short) this.dataMap.getInt(key);
481 | }
482 |
483 | @Override
484 | public short[] getShortArray(String key) {
485 | long[] values = this.dataMap.getLongArray(key);
486 | if(values == null){
487 | return null;
488 | }
489 |
490 | short[] shorts = new short[values.length];
491 | for(int i = 0; i < values.length; i++){
492 | shorts[i] = (short) values[i];
493 | }
494 | return shorts;
495 | }
496 |
497 | @Override
498 | public > T getShortList(String key, Class listtype) {
499 | long[] values = this.dataMap.getLongArray(key);
500 | if(values == null){
501 | return null;
502 | }
503 |
504 | T shorts = null;
505 | try {
506 | shorts = listtype.newInstance();
507 | for (int i = 0; i < values.length; i++) {
508 | shorts.add((short) values[i]);
509 | }
510 | } catch (Exception e){};
511 | return shorts;
512 | }
513 |
514 | @Override
515 | public int getInt(String key) {
516 | return this.dataMap.getInt(key);
517 | }
518 |
519 | @Override
520 | public int[] getIntArray(String key) {
521 | long[] values = this.dataMap.getLongArray(key);
522 | if(values == null){
523 | return null;
524 | }
525 |
526 | int[] ints = new int[values.length];
527 | for(int i = 0; i < ints.length; i++){
528 | ints[i] = (int) values[i];
529 | }
530 | return ints;
531 | }
532 |
533 | @Override
534 | public > T getIntList(String key, Class listtype) {
535 | long[] values = this.dataMap.getLongArray(key);
536 | if(values == null){
537 | return null;
538 | }
539 |
540 | T ints = null;
541 | try {
542 | ints = listtype.newInstance();
543 | for (int i = 0; i < values.length; i++) {
544 | ints.add((int) values[i]);
545 | }
546 | } catch (Exception e){};
547 | return ints;
548 | }
549 |
550 | @Override
551 | public long getLong(String key) {
552 | return this.dataMap.getLong(key);
553 | }
554 |
555 | @Override
556 | public long[] getLongArray(String key) {
557 | return this.dataMap.getLongArray(key);
558 | }
559 |
560 | @Override
561 | public > T getLongList(String key, Class listtype) {
562 | long[] values = this.dataMap.getLongArray(key);
563 | if(values == null){
564 | return null;
565 | }
566 |
567 | T longs = null;
568 | try {
569 | longs = listtype.newInstance();
570 | for (int i = 0; i < values.length; i++) {
571 | longs.add(values[i]);
572 | }
573 | } catch (Exception e){};
574 | return longs;
575 | }
576 |
577 | @Override
578 | public double getDouble(String key) {
579 | return this.dataMap.getDouble(key);
580 | }
581 |
582 | @Override
583 | public double[] getDoubleArray(String key) {
584 | float[] values = this.dataMap.getFloatArray(key);
585 | if(values == null){
586 | return null;
587 | }
588 |
589 | double[] doubles = new double[values.length];
590 | for(int i = 0; i < doubles.length; i++){
591 | doubles[i] = Double.parseDouble(Float.toString(values[i]));
592 | }
593 | return doubles;
594 | }
595 |
596 | @Override
597 | public > T getDoubleList(String key, Class listtype) {
598 | float[] values = this.dataMap.getFloatArray(key);
599 | if(values == null){
600 | return null;
601 | }
602 |
603 | T doubles = null;
604 | try {
605 | doubles = listtype.newInstance();
606 | for (int i = 0; i < values.length; i++) {
607 | doubles.add(Double.parseDouble(Float.toString(values[i])));
608 | }
609 | } catch (Exception e){};
610 | return doubles;
611 | }
612 |
613 | @Override
614 | public float getFloat(String key) {
615 | return this.dataMap.getFloat(key);
616 | }
617 |
618 | @Override
619 | public float[] getFloatArray(String key) {
620 | return this.dataMap.getFloatArray(key);
621 | }
622 |
623 | @Override
624 | public > T getFloatList(String key, Class listtype) {
625 | float[] values = this.dataMap.getFloatArray(key);
626 | if(values == null){
627 | return null;
628 | }
629 |
630 | T floats = null;
631 | try {
632 | floats = listtype.newInstance();
633 | for (int i = 0; i < values.length; i++) {
634 | floats.add(values[i]);
635 | }
636 | } catch (Exception e){};
637 | return floats;
638 | }
639 | }
640 |
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/wrappers/android/ParcelWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.wrappers.android;
17 |
18 | import android.os.Bundle;
19 | import android.os.Parcel;
20 | import com.larswerkman.boxer.Boxer;
21 |
22 | import java.lang.reflect.Array;
23 | import java.lang.reflect.Method;
24 | import java.util.List;
25 |
26 | /**
27 | *
28 | * ParcelWrapper defines how the Boxer class should write to a {@link android.os.Parcel}
29 | *
30 | * ParcelWrapper should only be used when you don't
31 | * change the order you read and write to it.
32 | */
33 | public class ParcelWrapper extends Boxer {
34 |
35 | Parcel parcel;
36 |
37 | public ParcelWrapper(Parcel object){
38 | super(object);
39 | parcel = object;
40 | }
41 |
42 | @Override
43 | public void add(String key, Object value) {
44 | this.parcel.writeBundle(serialize(new BundleWrapper(new Bundle()), value));
45 | }
46 |
47 | @Override
48 | public void addArray(String key, Object[] value) {
49 | this.parcel.writeInt(value.length);
50 | for(Object boxable : value) {
51 | this.parcel.writeBundle(serialize(new BundleWrapper(new Bundle()), boxable));
52 | }
53 | }
54 |
55 | @Override
56 | public void addList(String key, List> value) {
57 | addArray(key, value.toArray());
58 | }
59 |
60 | @Override
61 | public void addEnum(String key, Enum value) {
62 | this.parcel.writeString(value.name());
63 | }
64 |
65 | @Override
66 | public void addEnumArray(String key, Enum[] value) {
67 | String[] strings = new String[value.length];
68 | for(int i = 0; i < value.length; i++){
69 | strings[i] = value[i].name();
70 | }
71 | this.parcel.writeStringArray(strings);
72 | }
73 |
74 | @Override
75 | public void addEnumList(String key, List extends Enum> value) {
76 | String[] strings = new String[value.size()];
77 | for(int i = 0; i < value.size(); i++){
78 | strings[i] = value.get(i).name();
79 | }
80 | this.parcel.writeStringArray(strings);
81 | }
82 |
83 | @Override
84 | public void addString(String key, String value) {
85 | this.parcel.writeString(value);
86 | }
87 |
88 | @Override
89 | public void addStringArray(String key, String[] value) {
90 | this.parcel.writeStringArray(value);
91 | }
92 |
93 | @Override
94 | public void addStringList(String key, List value) {
95 | this.parcel.writeStringList(value);
96 | }
97 |
98 | @Override
99 | public void addBoolean(String key, boolean value) {
100 | this.parcel.writeByte(value ? Byte.MAX_VALUE : Byte.MIN_VALUE);
101 | }
102 |
103 | @Override
104 | public void addBooleanArray(String key, boolean[] value) {
105 | this.parcel.writeBooleanArray(value);
106 | }
107 |
108 | @Override
109 | public void addBooleanList(String key, List value) {
110 | boolean[] bools = new boolean[value.size()];
111 | for(int i = 0; i < value.size(); i++){
112 | bools[i] = value.get(i);
113 | }
114 | this.parcel.writeBooleanArray(bools);
115 | }
116 |
117 | @Override
118 | public void addByte(String key, byte value) {
119 | this.parcel.writeByte(value);
120 | }
121 |
122 | @Override
123 | public void addByteArray(String key, byte[] value) {
124 | this.parcel.writeByteArray(value);
125 | }
126 |
127 | @Override
128 | public void addByteList(String key, List value) {
129 | byte[] bytes = new byte[value.size()];
130 | for(int i = 0; i < value.size(); i++){
131 | bytes[i] = value.get(i);
132 | }
133 | this.parcel.writeByteArray(bytes);
134 | }
135 |
136 | @Override
137 | public void addChar(String key, char value) {
138 | this.parcel.writeString(String.valueOf(value));
139 | }
140 |
141 | @Override
142 | public void addCharArray(String key, char[] value) {
143 | this.parcel.writeCharArray(value);
144 | }
145 |
146 | @Override
147 | public void addCharList(String key, List value) {
148 | char[] chars = new char[value.size()];
149 | for(int i = 0; i < value.size(); i++){
150 | chars[i] = value.get(i);
151 | }
152 | this.parcel.writeCharArray(chars);
153 | }
154 |
155 | @Override
156 | public void addShort(String key, short value) {
157 | this.parcel.writeInt((int) value);
158 | }
159 |
160 | @Override
161 | public void addShortArray(String key, short[] value) {
162 | int[] shorts = new int[value.length];
163 | for(int i = 0; i < value.length; i++){
164 | shorts[i] = (int) value[i];
165 | }
166 | this.parcel.writeIntArray(shorts);
167 | }
168 |
169 | @Override
170 | public void addShortList(String key, List value) {
171 | int[] shorts = new int[value.size()];
172 | for(int i = 0; i < value.size(); i++){
173 | shorts[i] = (int) value.get(i);
174 | }
175 | this.parcel.writeIntArray(shorts);
176 | }
177 |
178 | @Override
179 | public void addInt(String key, int value) {
180 | this.parcel.writeInt(value);
181 | }
182 |
183 | @Override
184 | public void addIntArray(String key, int[] value) {
185 | this.parcel.writeIntArray(value);
186 | }
187 |
188 | @Override
189 | public void addIntList(String key, List value) {
190 | int[] ints = new int[value.size()];
191 | for(int i = 0; i < value.size(); i++){
192 | ints[i] = value.get(i);
193 | }
194 | this.parcel.writeIntArray(ints);
195 | }
196 |
197 | @Override
198 | public void addLong(String key, long value) {
199 | this.parcel.writeLong(value);
200 | }
201 |
202 | @Override
203 | public void addLongArray(String key, long[] value) {
204 | this.parcel.writeLongArray(value);
205 | }
206 |
207 | @Override
208 | public void addLongList(String key, List value) {
209 | long[] longs = new long[value.size()];
210 | for(int i = 0; i < value.size(); i++){
211 | longs[i] = value.get(i);
212 | }
213 | this.parcel.writeLongArray(longs);
214 | }
215 |
216 | @Override
217 | public void addDouble(String key, double value) {
218 | this.parcel.writeDouble(value);
219 | }
220 |
221 | @Override
222 | public void addDoubleArray(String key, double[] value) {
223 | this.parcel.writeDoubleArray(value);
224 | }
225 |
226 | @Override
227 | public void addDoubleList(String key, List value) {
228 | double[] doubles = new double[value.size()];
229 | for(int i = 0; i < value.size(); i++){
230 | doubles[i] = value.get(i);
231 | }
232 | this.parcel.writeDoubleArray(doubles);
233 | }
234 |
235 | @Override
236 | public void addFloat(String key, float value) {
237 | this.parcel.writeFloat(value);
238 | }
239 |
240 | @Override
241 | public void addFloatArray(String key, float[] value) {
242 | this.parcel.writeFloatArray(value);
243 | }
244 |
245 | @Override
246 | public void addFloatList(String key, List value) {
247 | float[] floats = new float[value.size()];
248 | for(int i = 0; i < value.size(); i++){
249 | floats[i] = value.get(i);
250 | }
251 | this.parcel.writeFloatArray(floats);
252 | }
253 |
254 | @Override
255 | public T get(String key, Class clazz) {
256 | Bundle bundle = this.parcel.readBundle();
257 | if(bundle == null || bundle.isEmpty()){
258 | return null;
259 | }
260 |
261 | return deserialize(new BundleWrapper(bundle), clazz);
262 | }
263 |
264 | @Override
265 | public T[] getArray(String key, Class clazz) {
266 | int size = this.parcel.readInt();
267 | if(size == 0){
268 | return null;
269 | }
270 |
271 | T[] boxables = (T[]) Array.newInstance(clazz, size);
272 | for(int i = 0; i < size; i++){
273 | boxables[i] = deserialize(new BundleWrapper(this.parcel.readBundle()), clazz);
274 | }
275 | return boxables;
276 | }
277 |
278 | @Override
279 | public > E getList(String key, Class clazz, Class listtype) {
280 | int size = this.parcel.readInt();
281 | if(size == 0){
282 | return null;
283 | }
284 |
285 | E boxables = null;
286 | try {
287 | boxables = listtype.newInstance();
288 | for (int i = 0; i < size; i++) {
289 | boxables.add(deserialize(new BundleWrapper(this.parcel.readBundle()), clazz));
290 | }
291 | } catch (Exception e){};
292 | return boxables;
293 | }
294 |
295 | public T retrieveEnum(String value, Class clazz){
296 | T en = null;
297 | try{
298 | Method method = clazz.getMethod("valueOf", String.class);
299 | en = (T) method.invoke(null, value);
300 | } catch (Exception e){}
301 | return en;
302 | }
303 |
304 | @Override
305 | public T getEnum(String key, Class clazz) {
306 | return retrieveEnum(this.parcel.readString(), clazz);
307 | }
308 |
309 | @Override
310 | public T[] getEnumArray(String key, Class clazz) {
311 | String[] values = this.parcel.createStringArray();
312 | if(values.length == 0){
313 | return null;
314 | }
315 |
316 | T[] enums = (T[]) Array.newInstance(clazz, values.length);
317 | for(int i = 0; i < values.length; i++){
318 | enums[i] = retrieveEnum(values[i], clazz);
319 | }
320 | return enums;
321 | }
322 |
323 | @Override
324 | public > E getEnumList(String key, Class clazz, Class listtype) {
325 | String[] values = this.parcel.createStringArray();
326 | if(values.length == 0){
327 | return null;
328 | }
329 |
330 | E enums = null;
331 | try {
332 | enums = listtype.newInstance();
333 | for (int i = 0; i < values.length; i++) {
334 | enums.add(retrieveEnum(values[i], clazz));
335 | }
336 | } catch (Exception e){};
337 | return enums;
338 | }
339 |
340 | @Override
341 | public String getString(String key) {
342 | return this.parcel.readString();
343 | }
344 |
345 | @Override
346 | public String[] getStringArray(String key) {
347 | String[] values = this.parcel.createStringArray();
348 | if(values.length == 0){
349 | return null;
350 | }
351 |
352 | return values;
353 | }
354 |
355 | @Override
356 | public > T getStringList(String key, Class listtype) {
357 | T list = null;
358 | try {
359 | list = listtype.newInstance();
360 | } catch (Exception e){}
361 | this.parcel.readStringList(list);
362 | if(list == null || list.size() == 0){
363 | return null;
364 | }
365 |
366 | return list;
367 | }
368 |
369 | @Override
370 | public boolean getBoolean(String key) {
371 | byte bytes = parcel.readByte();
372 | return bytes > Byte.MIN_VALUE && bytes != 0;
373 | }
374 |
375 | @Override
376 | public boolean[] getBooleanArray(String key) {
377 | boolean[] values = this.parcel.createBooleanArray();
378 | if(values.length == 0){
379 | return null;
380 | }
381 |
382 | return values;
383 | }
384 |
385 | @Override
386 | public > T getBooleanList(String key, Class listtype) {
387 | boolean[] values = this.parcel.createBooleanArray();
388 | if(values.length == 0){
389 | return null;
390 | }
391 |
392 | T booleans = null;
393 | try {
394 | booleans = listtype.newInstance();
395 | for (int i = 0; i < values.length; i++) {
396 | booleans.add(values[i]);
397 | }
398 | } catch (Exception e){};
399 | return booleans;
400 | }
401 |
402 | @Override
403 | public byte getByte(String key) {
404 | return this.parcel.readByte();
405 | }
406 |
407 | @Override
408 | public byte[] getByteArray(String key) {
409 | byte[] values = this.parcel.createByteArray();
410 | if(values.length == 0){
411 | return null;
412 | }
413 |
414 | return values;
415 | }
416 |
417 | @Override
418 | public > T getByteList(String key, Class listtype) {
419 | byte[] values = this.parcel.createByteArray();
420 | if(values.length == 0){
421 | return null;
422 | }
423 |
424 | T bytes = null;
425 | try {
426 | bytes = listtype.newInstance();
427 | for (int i = 0; i < values.length; i++) {
428 | bytes.add(values[i]);
429 | }
430 | } catch (Exception e){};
431 | return bytes;
432 | }
433 |
434 | @Override
435 | public char getChar(String key) {
436 | String value = this.parcel.readString();
437 | if(value == null){
438 | return 0;
439 | }
440 |
441 | return value.charAt(0);
442 | }
443 |
444 | @Override
445 | public char[] getCharArray(String key) {
446 | char[] values = this.parcel.createCharArray();
447 | if(values.length == 0){
448 | return null;
449 | }
450 |
451 | return values;
452 | }
453 |
454 | @Override
455 | public > T getCharList(String key, Class listtype) {
456 | char[] values = this.parcel.createCharArray();
457 | if(values.length == 0){
458 | return null;
459 | }
460 |
461 | T chars = null;
462 | try {
463 | chars = listtype.newInstance();
464 | for (int i = 0; i < values.length; i++) {
465 | chars.add(values[i]);
466 | }
467 | } catch (Exception e){};
468 | return chars;
469 | }
470 |
471 | @Override
472 | public short getShort(String key) {
473 | return (short) this.parcel.readInt();
474 | }
475 |
476 | @Override
477 | public short[] getShortArray(String key) {
478 | int[] values = this.parcel.createIntArray();
479 | if(values.length == 0){
480 | return null;
481 | }
482 |
483 | short[] shorts = new short[values.length];
484 | for(int i = 0; i < values.length; i++){
485 | shorts[i] = (short) values[i];
486 | }
487 | return shorts;
488 | }
489 |
490 | @Override
491 | public > T getShortList(String key, Class listtype) {
492 | int[] values = this.parcel.createIntArray();
493 | if(values.length == 0){
494 | return null;
495 | }
496 |
497 | T shorts = null;
498 | try {
499 | shorts = listtype.newInstance();
500 | for (int i = 0; i < values.length; i++) {
501 | shorts.add((short) values[i]);
502 | }
503 | } catch (Exception e){};
504 | return shorts;
505 | }
506 |
507 | @Override
508 | public int getInt(String key) {
509 | return this.parcel.readInt();
510 | }
511 |
512 | @Override
513 | public int[] getIntArray(String key) {
514 | int[] values = this.parcel.createIntArray();
515 | if(values.length == 0){
516 | return null;
517 | }
518 |
519 | return values;
520 | }
521 |
522 | @Override
523 | public > T getIntList(String key, Class listtype) {
524 | int[] values = this.parcel.createIntArray();
525 | if(values.length == 0){
526 | return null;
527 | }
528 |
529 | T ints = null;
530 | try {
531 | ints = listtype.newInstance();
532 | for (int i = 0; i < values.length; i++) {
533 | ints.add(values[i]);
534 | }
535 | } catch (Exception e){};
536 | return ints;
537 | }
538 |
539 | @Override
540 | public long getLong(String key) {
541 | return this.parcel.readLong();
542 | }
543 |
544 | @Override
545 | public long[] getLongArray(String key) {
546 | long[] values = this.parcel.createLongArray();
547 | if(values.length == 0){
548 | return null;
549 | }
550 |
551 | return values;
552 | }
553 |
554 | @Override
555 | public > T getLongList(String key, Class listtype) {
556 | long[] values = this.parcel.createLongArray();
557 | if(values.length == 0){
558 | return null;
559 | }
560 |
561 | T longs = null;
562 | try {
563 | longs = listtype.newInstance();
564 | for (int i = 0; i < values.length; i++) {
565 | longs.add(values[i]);
566 | }
567 | } catch (Exception e){};
568 | return longs;
569 | }
570 |
571 | @Override
572 | public double getDouble(String key) {
573 | return this.parcel.readDouble();
574 | }
575 |
576 | @Override
577 | public double[] getDoubleArray(String key) {
578 | double[] values = this.parcel.createDoubleArray();
579 | if(values.length == 0){
580 | return null;
581 | }
582 |
583 | return values;
584 | }
585 |
586 | @Override
587 | public > T getDoubleList(String key, Class listtype) {
588 | double[] values = this.parcel.createDoubleArray();
589 | if(values.length == 0){
590 | return null;
591 | }
592 |
593 | T doubles = null;
594 | try {
595 | doubles = listtype.newInstance();
596 | for (int i = 0; i < values.length; i++) {
597 | doubles.add(values[i]);
598 | }
599 | } catch (Exception e){};
600 | return doubles;
601 | }
602 |
603 | @Override
604 | public float getFloat(String key) {
605 | return this.parcel.readFloat();
606 | }
607 |
608 | @Override
609 | public float[] getFloatArray(String key) {
610 | float[] values = this.parcel.createFloatArray();
611 | if(values.length == 0){
612 | return null;
613 | }
614 |
615 | return values;
616 | }
617 |
618 | @Override
619 | public > T getFloatList(String key, Class listtype) {
620 | float[] values = this.parcel.createFloatArray();
621 | if(values.length == 0){
622 | return null;
623 | }
624 |
625 | T floats = null;
626 | try {
627 | floats = listtype.newInstance();
628 | for (int i = 0; i < values.length; i++) {
629 | floats.add(values[i]);
630 | }
631 | } catch (Exception e){};
632 | return floats;
633 | }
634 | }
635 |
--------------------------------------------------------------------------------
/boxer/src/main/java/com/larswerkman/boxer/wrappers/android/SQLiteWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Lars Werkman
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.larswerkman.boxer.wrappers.android;
17 |
18 | import android.content.ContentValues;
19 | import android.database.Cursor;
20 | import android.database.sqlite.SQLiteDatabase;
21 | import com.larswerkman.boxer.Boxer;
22 |
23 | import java.lang.reflect.Array;
24 | import java.lang.reflect.Method;
25 | import java.util.List;
26 |
27 | /**
28 | * Experimental wrapper to see if SQLite is possible.
29 | *
30 | * Created by lars on 24-04-15.
31 | */
32 | public class SQLiteWrapper extends Boxer {
33 |
34 | private static final String COLUMN_KEY = "key";
35 | private static final String COLUMN_VALUE = "value";
36 | private static final String COLUMN_INDEX = "pos";
37 |
38 | private static final String TABLE_BOXABLE = "boxable_table_boxer";
39 | private static final String TABLE_STRING = "string_table_boxer";
40 | private static final String TABLE_STRING_ARRAY = "string_array_table_boxer";
41 | private static final String TABLE_INTEGER = "integer_table_boxer";
42 | private static final String TABLE_INTEGER_ARRAY = "integer_array_table_boxer";
43 | private static final String TABLE_REAL = "real_table_boxer";
44 | private static final String TABLE_REAL_ARRAY = "real_array_table_boxer";
45 | private static final String TABLE_BLOB = "blob_table_boxer";
46 | private static final String TABLE_ARRAY_SIZE = "array_size_table_boxer";
47 |
48 | private static final String ID_SEPARATOR = ".";
49 | private static final String INDEX_SEPARATOR = "*";
50 |
51 | private String identifier = "";
52 | private SQLiteDatabase database;
53 |
54 | public SQLiteWrapper(SQLiteDatabase database) {
55 | super(database);
56 | this.database = database;
57 |
58 | createTable(TABLE_STRING, "TEXT");
59 | createArrayTable(TABLE_STRING_ARRAY, "TEXT");
60 | createTable(TABLE_INTEGER, "INTEGER");
61 | createArrayTable(TABLE_INTEGER_ARRAY, "INTEGER");
62 | createTable(TABLE_REAL, "REAL");
63 | createArrayTable(TABLE_REAL_ARRAY, "REAL");
64 | createTable(TABLE_BLOB, "BLOB");
65 | createTable(TABLE_ARRAY_SIZE, "INTEGER");
66 | createTable(TABLE_BOXABLE, "INTEGER");
67 | }
68 |
69 | private SQLiteWrapper(SQLiteDatabase database, String identifier){
70 | this(database);
71 | this.identifier = identifier;
72 | }
73 |
74 | private void createTable(String name, String type){
75 | String query = "CREATE TABLE IF NOT EXISTS "
76 | + name + "(" + COLUMN_KEY + " TEXT PRIMARY KEY,"
77 | + COLUMN_VALUE + " " + type + ");";
78 | this.database.execSQL(query);
79 | }
80 |
81 | private void createArrayTable(String name, String type){
82 | String query = "CREATE TABLE IF NOT EXISTS "
83 | + name + "(" + COLUMN_KEY + " TEXT,"
84 | + COLUMN_VALUE + " " + type + ","
85 | + COLUMN_INDEX + " INTEGER);";
86 | this.database.execSQL(query);
87 | }
88 |
89 | private void addKeyValue(String key, ContentValues value, String table){
90 | value.put(COLUMN_KEY, key);
91 | database.insertWithOnConflict(table, COLUMN_KEY, value, SQLiteDatabase.CONFLICT_REPLACE);
92 | }
93 |
94 | private void addKeyValues(String key, ContentValues[] values, String table){
95 | database.beginTransaction();
96 | database.delete(table, COLUMN_KEY + "=?", new String[]{key});
97 | for(ContentValues value : values){
98 | value.put(COLUMN_KEY, key);
99 | database.insert(table, null, value);
100 | }
101 | database.setTransactionSuccessful();
102 | database.endTransaction();
103 | }
104 |
105 | @Override
106 | public void add(String key, Object value) {
107 | ContentValues content = new ContentValues();
108 | content.put(COLUMN_VALUE, 1);
109 | addKeyValue(getIdentifier(key), content, TABLE_BOXABLE);
110 |
111 | serialize(new SQLiteWrapper(database, getIdentifier(key)), value);
112 | }
113 |
114 | @Override
115 | public void addArray(String key, Object[] value) {
116 | ContentValues content = new ContentValues();
117 | content.put(COLUMN_VALUE, value.length);
118 | addKeyValue(getIdentifier(key), content, TABLE_ARRAY_SIZE);
119 |
120 | for(int i = 0; i < value.length; i++){
121 | serialize(new SQLiteWrapper(database, getIdentifier(key, i)), value[i]);
122 | }
123 | }
124 |
125 | @Override
126 | public void addList(String key, List> value) {
127 | addArray(key, value.toArray());
128 | }
129 |
130 | @Override
131 | public void addEnum(String key, Enum value) {
132 | ContentValues content = new ContentValues();
133 | content.put(COLUMN_VALUE, value.name());
134 | addKeyValue(getIdentifier(key), content, TABLE_STRING);
135 | }
136 |
137 | @Override
138 | public void addEnumArray(String key, Enum[] value) {
139 | ContentValues[] contents = new ContentValues[value.length];
140 | for(int i = 0; i < value.length; i++){
141 | ContentValues content = new ContentValues();
142 | content.put(COLUMN_VALUE, value[i].name());
143 | content.put(COLUMN_INDEX, i);
144 | contents[i] = content;
145 | }
146 |
147 | addKeyValues(getIdentifier(key), contents, TABLE_STRING_ARRAY);
148 | }
149 |
150 | @Override
151 | public void addEnumList(String key, List extends Enum> value) {
152 | addEnumArray(key, (Enum[]) value.toArray());
153 | }
154 |
155 | @Override
156 | public void addString(String key, String value) {
157 | ContentValues content = new ContentValues();
158 | content.put(COLUMN_VALUE, value);
159 | addKeyValue(getIdentifier(key), content, TABLE_STRING);
160 | }
161 |
162 | @Override
163 | public void addStringArray(String key, String[] value) {
164 | ContentValues[] contents = new ContentValues[value.length];
165 | for(int i = 0; i < value.length; i++){
166 | ContentValues content = new ContentValues();
167 | content.put(COLUMN_VALUE, value[i]);
168 | content.put(COLUMN_INDEX, i);
169 | contents[i] = content;
170 | }
171 |
172 | addKeyValues(getIdentifier(key), contents, TABLE_STRING_ARRAY);
173 | }
174 |
175 | @Override
176 | public void addStringList(String key, List value) {
177 | addStringArray(key, (String[]) value.toArray());
178 | }
179 |
180 | @Override
181 | public void addBoolean(String key, boolean value) {
182 | ContentValues content = new ContentValues();
183 | content.put(COLUMN_VALUE, value);
184 | addKeyValue(getIdentifier(key), content, TABLE_INTEGER);
185 | }
186 |
187 | @Override
188 | public void addBooleanArray(String key, boolean[] value) {
189 | ContentValues[] contents = new ContentValues[value.length];
190 | for(int i = 0; i < value.length; i++){
191 | ContentValues content = new ContentValues();
192 | content.put(COLUMN_VALUE, value[i]);
193 | content.put(COLUMN_INDEX, i);
194 | contents[i] = content;
195 | }
196 |
197 | addKeyValues(getIdentifier(key), contents, TABLE_INTEGER_ARRAY);
198 | }
199 |
200 | @Override
201 | public void addBooleanList(String key, List value) {
202 | boolean[] booleans = new boolean[value.size()];
203 | for(int i = 0; i < value.size(); i++){
204 | booleans[i] = value.get(i);
205 | }
206 | addBooleanArray(key, booleans);
207 | }
208 |
209 | @Override
210 | public void addByte(String key, byte value) {
211 | ContentValues content = new ContentValues();
212 | content.put(COLUMN_VALUE, value);
213 | addKeyValue(getIdentifier(key), content, TABLE_INTEGER);
214 | }
215 |
216 | @Override
217 | public void addByteArray(String key, byte[] value) {
218 | ContentValues content = new ContentValues();
219 | content.put(COLUMN_VALUE, value);
220 | addKeyValue(getIdentifier(key), content, TABLE_BLOB);
221 | }
222 |
223 | @Override
224 | public void addByteList(String key, List value) {
225 | byte[] bytes = new byte[value.size()];
226 | for(int i = 0; i < value.size(); i++){
227 | bytes[i] = value.get(i);
228 | }
229 | addByteArray(key, bytes);
230 | }
231 |
232 | @Override
233 | public void addChar(String key, char value) {
234 | ContentValues content = new ContentValues();
235 | content.put(COLUMN_VALUE, (int) value);
236 | addKeyValue(getIdentifier(key), content, TABLE_INTEGER);
237 | }
238 |
239 | @Override
240 | public void addCharArray(String key, char[] value) {
241 | ContentValues[] contents = new ContentValues[value.length];
242 | for(int i = 0; i < value.length; i++){
243 | ContentValues content = new ContentValues();
244 | content.put(COLUMN_VALUE, (int) value[i]);
245 | content.put(COLUMN_INDEX, i);
246 | contents[i] = content;
247 | }
248 |
249 | addKeyValues(getIdentifier(key), contents, TABLE_INTEGER_ARRAY);
250 | }
251 |
252 | @Override
253 | public void addCharList(String key, List value) {
254 | char[] chars = new char[value.size()];
255 | for(int i = 0; i < value.size(); i++){
256 | chars[i] = value.get(i);
257 | }
258 | addCharArray(key, chars);
259 | }
260 |
261 | @Override
262 | public void addShort(String key, short value) {
263 | ContentValues content = new ContentValues();
264 | content.put(COLUMN_VALUE, value);
265 | addKeyValue(getIdentifier(key), content, TABLE_INTEGER);
266 | }
267 |
268 | @Override
269 | public void addShortArray(String key, short[] value) {
270 | ContentValues[] contents = new ContentValues[value.length];
271 | for(int i = 0; i < value.length; i++){
272 | ContentValues content = new ContentValues();
273 | content.put(COLUMN_VALUE, value[i]);
274 | content.put(COLUMN_INDEX, i);
275 | contents[i] = content;
276 | }
277 |
278 | addKeyValues(getIdentifier(key), contents, TABLE_INTEGER_ARRAY);
279 | }
280 |
281 | @Override
282 | public void addShortList(String key, List value) {
283 | short[] shorts = new short[value.size()];
284 | for(int i = 0; i < value.size(); i++){
285 | shorts[i] = value.get(i);
286 | }
287 | addShortArray(key, shorts);
288 | }
289 |
290 | @Override
291 | public void addInt(String key, int value) {
292 | ContentValues content = new ContentValues();
293 | content.put(COLUMN_VALUE, value);
294 | addKeyValue(getIdentifier(key), content, TABLE_INTEGER);
295 | }
296 |
297 | @Override
298 | public void addIntArray(String key, int[] value) {
299 | ContentValues[] contents = new ContentValues[value.length];
300 | for(int i = 0; i < value.length; i++){
301 | ContentValues content = new ContentValues();
302 | content.put(COLUMN_VALUE, value[i]);
303 | content.put(COLUMN_INDEX, i);
304 | contents[i] = content;
305 | }
306 |
307 | addKeyValues(getIdentifier(key), contents, TABLE_INTEGER_ARRAY);
308 | }
309 |
310 | @Override
311 | public void addIntList(String key, List value) {
312 | int[] ints = new int[value.size()];
313 | for(int i = 0; i < value.size(); i++){
314 | ints[i] = value.get(i);
315 | }
316 | addIntArray(key, ints);
317 | }
318 |
319 | @Override
320 | public void addLong(String key, long value) {
321 | ContentValues content = new ContentValues();
322 | content.put(COLUMN_VALUE, value);
323 | addKeyValue(getIdentifier(key), content, TABLE_INTEGER);
324 | }
325 |
326 | @Override
327 | public void addLongArray(String key, long[] value) {
328 | ContentValues[] contents = new ContentValues[value.length];
329 | for(int i = 0; i < value.length; i++){
330 | ContentValues content = new ContentValues();
331 | content.put(COLUMN_VALUE, value[i]);
332 | content.put(COLUMN_INDEX, i);
333 | contents[i] = content;
334 | }
335 |
336 | addKeyValues(getIdentifier(key), contents, TABLE_INTEGER_ARRAY);
337 | }
338 |
339 | @Override
340 | public void addLongList(String key, List value) {
341 | long[] longs = new long[value.size()];
342 | for(int i = 0; i < value.size(); i++){
343 | longs[i] = value.get(i);
344 | }
345 | addLongArray(key, longs);
346 | }
347 |
348 | @Override
349 | public void addDouble(String key, double value) {
350 | ContentValues content = new ContentValues();
351 | content.put(COLUMN_VALUE, value);
352 | addKeyValue(getIdentifier(key), content, TABLE_INTEGER);
353 | }
354 |
355 | @Override
356 | public void addDoubleArray(String key, double[] value) {
357 | ContentValues[] contents = new ContentValues[value.length];
358 | for(int i = 0; i < value.length; i++){
359 | ContentValues content = new ContentValues();
360 | content.put(COLUMN_VALUE, value[i]);
361 | content.put(COLUMN_INDEX, i);
362 | contents[i] = content;
363 | }
364 |
365 | addKeyValues(getIdentifier(key), contents, TABLE_INTEGER_ARRAY);
366 | }
367 |
368 | @Override
369 | public void addDoubleList(String key, List value) {
370 | double[] doubles = new double[value.size()];
371 | for(int i = 0; i < value.size(); i++){
372 | doubles[i] = value.get(i);
373 | }
374 | addDoubleArray(key, doubles);
375 | }
376 |
377 | @Override
378 | public void addFloat(String key, float value) {
379 | ContentValues content = new ContentValues();
380 | content.put(COLUMN_VALUE, value);
381 | addKeyValue(getIdentifier(key), content, TABLE_REAL);
382 | }
383 |
384 | @Override
385 | public void addFloatArray(String key, float[] value) {
386 | ContentValues[] contents = new ContentValues[value.length];
387 | for(int i = 0; i < value.length; i++){
388 | ContentValues content = new ContentValues();
389 | content.put(COLUMN_VALUE, value[i]);
390 | content.put(COLUMN_INDEX, i);
391 | contents[i] = content;
392 | }
393 |
394 | addKeyValues(getIdentifier(key), contents, TABLE_REAL_ARRAY);
395 | }
396 |
397 | @Override
398 | public void addFloatList(String key, List value) {
399 | float[] floats = new float[value.size()];
400 | for(int i = 0; i < value.size(); i++){
401 | floats[i] = value.get(i);
402 | }
403 | addFloatArray(key, floats);
404 | }
405 |
406 | private Cursor getKeyValue(String key, String table){
407 | return database.query(table, new String[]{COLUMN_KEY, COLUMN_VALUE}, COLUMN_KEY + "=?", new String[]{key}, null, null, null);
408 | }
409 |
410 | private Cursor getKeyValues(String key, String table){
411 | return database.query(table, new String[]{COLUMN_KEY, COLUMN_VALUE, COLUMN_INDEX}, COLUMN_KEY + "=?", new String[]{key}, null, null, COLUMN_INDEX);
412 | }
413 |
414 | @Override
415 | public T get(String key, Class clazz) {
416 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_BOXABLE);
417 | if(cursor == null || cursor.getCount() == 0){
418 | return null;
419 | }
420 |
421 | return deserialize(new SQLiteWrapper(database, getIdentifier(key)), clazz);
422 | }
423 |
424 | @Override
425 | public T[] getArray(String key, Class clazz) {
426 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_ARRAY_SIZE);
427 | if(cursor == null || cursor.getCount() == 0){
428 | return null;
429 | }
430 | cursor.moveToFirst();
431 | int size = cursor.getInt(1);
432 |
433 | T[] boxables = (T[]) Array.newInstance(clazz, size);
434 | for(int i = 0; i < size; i++){
435 | boxables[i] = deserialize(new SQLiteWrapper(database, getIdentifier(key, i)), clazz);
436 | }
437 | return boxables;
438 | }
439 |
440 | @Override
441 | public > E getList(String key, Class clazz, Class listtype) {
442 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_ARRAY_SIZE);
443 | if(cursor == null || cursor.getCount() == 0){
444 | return null;
445 | }
446 | cursor.moveToFirst();
447 | int size = cursor.getInt(1);
448 |
449 | E boxables = null;
450 | try {
451 | boxables = listtype.newInstance();
452 | for (int i = 0; i < size; i++) {
453 | boxables.add(deserialize(new SQLiteWrapper(database, getIdentifier(key, i)), clazz));
454 | }
455 | } catch (Exception e){};
456 | return boxables;
457 | }
458 |
459 | private T retrieveEnum(String value, Class clazz){
460 | T en = null;
461 | try{
462 | Method method = clazz.getMethod("valueOf", String.class);
463 | en = (T) method.invoke(null, value);
464 | } catch (Exception e){}
465 | return en;
466 | }
467 |
468 | @Override
469 | public T getEnum(String key, Class clazz) {
470 | return retrieveEnum(getString(key), clazz);
471 | }
472 |
473 | @Override
474 | public T[] getEnumArray(String key, Class clazz) {
475 | String[] values = getStringArray(key);
476 | if(values == null){
477 | return null;
478 | }
479 |
480 | T[] enums = (T[]) Array.newInstance(clazz, values.length);
481 | for(int i = 0; i < values.length; i++){
482 | enums[i] = retrieveEnum(values[i], clazz);
483 | }
484 | return enums;
485 | }
486 |
487 | @Override
488 | public > E getEnumList(String key, Class clazz, Class listtype) {
489 | String[] values = getStringArray(key);
490 | if(values == null){
491 | return null;
492 | }
493 | E enums = null;
494 | try {
495 | enums = listtype.newInstance();
496 | for (int i = 0; i < values.length; i++) {
497 | enums.add(retrieveEnum(values[i], clazz));
498 | }
499 | } catch (Exception e){};
500 | return enums;
501 | }
502 |
503 | @Override
504 | public String getString(String key) {
505 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_STRING);
506 | if(cursor == null || cursor.getCount() == 0){
507 | return null;
508 | }
509 |
510 | cursor.moveToFirst();
511 | return cursor.getString(1);
512 | }
513 |
514 | @Override
515 | public String[] getStringArray(String key) {
516 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_STRING_ARRAY);
517 | if(cursor == null || cursor.getCount() == 0){
518 | return null;
519 | }
520 |
521 | String[] values = new String[cursor.getCount()];
522 | while(cursor.moveToNext()){
523 | values[cursor.getPosition()] = cursor.getString(1);
524 | }
525 | return values;
526 | }
527 |
528 | @Override
529 | public > T getStringList(String key, Class listtype) {
530 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_STRING_ARRAY);
531 | if(cursor == null || cursor.getCount() == 0){
532 | return null;
533 | }
534 |
535 | T strings = null;
536 | try {
537 | strings = listtype.newInstance();
538 | while(cursor.moveToNext()) {
539 | strings.add(cursor.getString(1));
540 | }
541 | } catch (Exception e){};
542 | return strings;
543 | }
544 |
545 | @Override
546 | public boolean getBoolean(String key) {
547 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_INTEGER);
548 | if(cursor == null || cursor.getCount() == 0){
549 | return false;
550 | }
551 |
552 | cursor.moveToFirst();
553 | return cursor.getInt(1) > 0;
554 | }
555 |
556 | @Override
557 | public boolean[] getBooleanArray(String key) {
558 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
559 | if(cursor == null || cursor.getCount() == 0){
560 | return null;
561 | }
562 |
563 | boolean[] values = new boolean[cursor.getCount()];
564 | while(cursor.moveToNext()){
565 | values[cursor.getPosition()] = cursor.getInt(1) > 0;
566 | }
567 | return values;
568 | }
569 |
570 | @Override
571 | public > T getBooleanList(String key, Class listtype) {
572 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
573 | if(cursor == null || cursor.getCount() == 0){
574 | return null;
575 | }
576 |
577 | T booleans = null;
578 | try {
579 | booleans = listtype.newInstance();
580 | while(cursor.moveToNext()) {
581 | booleans.add(cursor.getInt(1) > 0);
582 | }
583 | } catch (Exception e){};
584 | return booleans;
585 | }
586 |
587 | @Override
588 | public byte getByte(String key) {
589 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_INTEGER);
590 | if(cursor == null || cursor.getCount() == 0){
591 | return 0;
592 | }
593 |
594 | cursor.moveToFirst();
595 | return (byte) cursor.getInt(1);
596 | }
597 |
598 | @Override
599 | public byte[] getByteArray(String key) {
600 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_BLOB);
601 | if(cursor == null || cursor.getCount() == 0){
602 | return null;
603 | }
604 |
605 | cursor.moveToFirst();
606 | return cursor.getBlob(1);
607 | }
608 |
609 | @Override
610 | public > T getByteList(String key, Class listtype) {
611 | byte[] values = getByteArray(key);
612 | if(values == null){
613 | return null;
614 | }
615 |
616 | T bytes = null;
617 | try {
618 | bytes = listtype.newInstance();
619 | for(byte b : values){
620 | bytes.add(b);
621 | }
622 | } catch (Exception e){};
623 | return bytes;
624 | }
625 |
626 | @Override
627 | public char getChar(String key) {
628 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_INTEGER);
629 | if(cursor == null || cursor.getCount() == 0){
630 | return 0;
631 | }
632 |
633 | cursor.moveToFirst();
634 | return (char) cursor.getInt(1);
635 | }
636 |
637 | @Override
638 | public char[] getCharArray(String key) {
639 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
640 | if(cursor == null || cursor.getCount() == 0){
641 | return null;
642 | }
643 |
644 | char[] values = new char[cursor.getCount()];
645 | while(cursor.moveToNext()){
646 | values[cursor.getPosition()] = (char) cursor.getInt(1);
647 | }
648 | return values;
649 | }
650 |
651 | @Override
652 | public > T getCharList(String key, Class listtype) {
653 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
654 | if(cursor == null || cursor.getCount() == 0){
655 | return null;
656 | }
657 |
658 | T chars = null;
659 | try {
660 | chars = listtype.newInstance();
661 | while(cursor.moveToNext()) {
662 | chars.add((char) cursor.getInt(1));
663 | }
664 | } catch (Exception e){};
665 | return chars;
666 | }
667 |
668 | @Override
669 | public short getShort(String key) {
670 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_INTEGER);
671 | if(cursor == null || cursor.getCount() == 0){
672 | return 0;
673 | }
674 |
675 | cursor.moveToFirst();
676 | return cursor.getShort(1);
677 | }
678 |
679 | @Override
680 | public short[] getShortArray(String key) {
681 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
682 | if(cursor == null || cursor.getCount() == 0){
683 | return null;
684 | }
685 |
686 | short[] values = new short[cursor.getCount()];
687 | while(cursor.moveToNext()){
688 | values[cursor.getPosition()] = cursor.getShort(1);
689 | }
690 | return values;
691 | }
692 |
693 | @Override
694 | public > T getShortList(String key, Class listtype) {
695 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
696 | if(cursor == null || cursor.getCount() == 0){
697 | return null;
698 | }
699 |
700 | T shorts = null;
701 | try {
702 | shorts = listtype.newInstance();
703 | while(cursor.moveToNext()) {
704 | shorts.add(cursor.getShort(1));
705 | }
706 | } catch (Exception e){};
707 | return shorts;
708 | }
709 |
710 | @Override
711 | public int getInt(String key) {
712 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_INTEGER);
713 | if(cursor == null || cursor.getCount() == 0){
714 | return 0;
715 | }
716 |
717 | cursor.moveToFirst();
718 | return cursor.getInt(1);
719 | }
720 |
721 | @Override
722 | public int[] getIntArray(String key) {
723 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
724 | if(cursor == null || cursor.getCount() == 0){
725 | return null;
726 | }
727 |
728 | int[] values = new int[cursor.getCount()];
729 | while(cursor.moveToNext()){
730 | values[cursor.getPosition()] = cursor.getInt(1);
731 | }
732 | return values;
733 | }
734 |
735 | @Override
736 | public > T getIntList(String key, Class listtype) {
737 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
738 | if(cursor == null || cursor.getCount() == 0){
739 | return null;
740 | }
741 |
742 | T ints = null;
743 | try {
744 | ints = listtype.newInstance();
745 | while(cursor.moveToNext()) {
746 | ints.add(cursor.getInt(1));
747 | }
748 | } catch (Exception e){};
749 | return ints;
750 | }
751 |
752 | @Override
753 | public long getLong(String key) {
754 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_INTEGER);
755 | if(cursor == null || cursor.getCount() == 0){
756 | return 0;
757 | }
758 |
759 | cursor.moveToFirst();
760 | return cursor.getLong(1);
761 | }
762 |
763 | @Override
764 | public long[] getLongArray(String key) {
765 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
766 | if(cursor == null || cursor.getCount() == 0){
767 | return null;
768 | }
769 |
770 | long[] values = new long[cursor.getCount()];
771 | while(cursor.moveToNext()){
772 | values[cursor.getPosition()] = cursor.getLong(1);
773 | }
774 | return values;
775 | }
776 |
777 | @Override
778 | public > T getLongList(String key, Class listtype) {
779 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
780 | if(cursor == null || cursor.getCount() == 0){
781 | return null;
782 | }
783 |
784 | T longs = null;
785 | try {
786 | longs = listtype.newInstance();
787 | while(cursor.moveToNext()) {
788 | longs.add(cursor.getLong(1));
789 | }
790 | } catch (Exception e){};
791 | return longs;
792 | }
793 |
794 | @Override
795 | public double getDouble(String key) {
796 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_INTEGER);
797 | if(cursor == null || cursor.getCount() == 0){
798 | return 0;
799 | }
800 |
801 | cursor.moveToFirst();
802 | return cursor.getDouble(1);
803 | }
804 |
805 | @Override
806 | public double[] getDoubleArray(String key) {
807 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
808 | if(cursor == null || cursor.getCount() == 0){
809 | return null;
810 | }
811 |
812 | double[] values = new double[cursor.getCount()];
813 | while(cursor.moveToNext()){
814 | values[cursor.getPosition()] = cursor.getDouble(1);
815 | }
816 | return values;
817 | }
818 |
819 | @Override
820 | public > T getDoubleList(String key, Class listtype) {
821 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_INTEGER_ARRAY);
822 | if(cursor == null || cursor.getCount() == 0){
823 | return null;
824 | }
825 |
826 | T doubles = null;
827 | try {
828 | doubles = listtype.newInstance();
829 | while(cursor.moveToNext()) {
830 | doubles.add(cursor.getDouble(1));
831 | }
832 | } catch (Exception e){};
833 | return doubles;
834 | }
835 |
836 | @Override
837 | public float getFloat(String key) {
838 | Cursor cursor = getKeyValue(getIdentifier(key), TABLE_REAL);
839 | if(cursor == null || cursor.getCount() == 0){
840 | return 0;
841 | }
842 |
843 | cursor.moveToFirst();
844 | return cursor.getFloat(1);
845 | }
846 |
847 | @Override
848 | public float[] getFloatArray(String key) {
849 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_REAL_ARRAY);
850 | if(cursor == null || cursor.getCount() == 0){
851 | return null;
852 | }
853 |
854 | float[] values = new float[cursor.getCount()];
855 | while(cursor.moveToNext()){
856 | values[cursor.getPosition()] = cursor.getFloat(1);
857 | }
858 | return values;
859 | }
860 |
861 | @Override
862 | public > T getFloatList(String key, Class listtype) {
863 | Cursor cursor = getKeyValues(getIdentifier(key), TABLE_REAL_ARRAY);
864 | if(cursor == null || cursor.getCount() == 0){
865 | return null;
866 | }
867 |
868 | T floats = null;
869 | try {
870 | floats = listtype.newInstance();
871 | while(cursor.moveToNext()) {
872 | floats.add(cursor.getFloat(1));
873 | }
874 | } catch (Exception e){};
875 | return floats;
876 | }
877 |
878 | private String getIdentifier(String key){
879 | return identifier + omitSeparators(key) + ID_SEPARATOR;
880 | }
881 |
882 | private String getIdentifier(String key, int position){
883 | return identifier + omitSeparators(key) + INDEX_SEPARATOR + position + ID_SEPARATOR;
884 | }
885 |
886 | private String omitSeparators(String key){
887 | key = key.replace(ID_SEPARATOR, "?.?");
888 | key = key.replace(INDEX_SEPARATOR, "?*?");
889 | return key;
890 | }
891 | }
892 |
--------------------------------------------------------------------------------
/boxer/src/main/resources/META-INF/services/javax.annotation.processing.Processor:
--------------------------------------------------------------------------------
1 | com.larswerkman.boxer.internal.BoxerProcessor
2 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/AbstractWrapperTest.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer;
2 |
3 | import com.google.common.primitives.*;
4 | import com.larswerkman.boxer.boxables.*;
5 | import com.larswerkman.boxer.enums.PrimaryEnum;
6 | import org.assertj.core.api.Assertions;
7 | import org.junit.After;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 | import org.junit.runner.RunWith;
11 | import org.robolectric.RobolectricTestRunner;
12 | import org.robolectric.annotation.Config;
13 |
14 | import java.util.*;
15 |
16 | /**
17 | * Created by lars on 22-04-15.
18 | */
19 | @RunWith(RobolectricTestRunner.class)
20 | @Config(manifest = Config.NONE)
21 | public abstract class AbstractWrapperTest {
22 |
23 | public Boxer> boxer;
24 |
25 | private static final String KEY = "key";
26 |
27 | private static final Date DATE_ADAPTER = new Date();
28 | private static final Date[] DATE_ADAPTER_ARRAY = {new Date(), new Date()};
29 |
30 | private static final PrimaryBoxable BOXABLE = new PrimaryBoxable().setup();
31 | private static final PrimaryBoxable[] BOXABLE_ARRAY = {new PrimaryBoxable().setup(), new PrimaryBoxable().setup()};
32 |
33 | private static final PrimaryEnum ENUM = PrimaryEnum.TO_BE;
34 | private static final PrimaryEnum[] ENUM_ARRAY = {PrimaryEnum.TO_BE, PrimaryEnum.NOT_TO_BE};
35 |
36 | private static final String STRING = "string";
37 | private static final String[] STRING_ARRAY = {"first", "second"};
38 |
39 | private static final boolean BOOLEAN = true;
40 | private static final boolean[] BOOLEAN_ARRAY = {true, false};
41 |
42 | private static final byte BYTE = 0xA;
43 | private static final byte[] BYTE_ARRAY = {0xA, 0xB};
44 |
45 | private static final char CHARACTER = 'A';
46 | private static final char[] CHARACTER_ARRAY = {'A', 'B'};
47 |
48 | private static final short SHORT = 99;
49 | private static final short[] SHORT_ARRAY = {98, 99};
50 |
51 | private static final int INTEGER = 99;
52 | private static final int[] INTEGER_ARRAY = {98, 99};
53 |
54 | private static final long LONG = 99l;
55 | private static final long[] LONG_ARRAY = {98l, 99l};
56 |
57 | private static final double DOUBLE = 0.99;
58 | private static final double[] DOUBLE_ARRAY = {0.98, 0.99};
59 |
60 | private static final float FLOAT = 0.99f;
61 | private static final float[] FLOAT_ARRAY = {0.98f, 0.99f};
62 |
63 | private static final ListBoxable LIST_BOXABLE = new ListBoxable().setup();
64 | private static final TransientBoxable TRANSIENT_BOXABLE = new TransientBoxable().setup();
65 | private static final AccessBoxable ACCESS_BOXABLE = new AccessBoxable().setup();
66 | private static final InheritancePrimaryBoxable INHERITANCE_PRIMARY_BOXABLE = new InheritancePrimaryBoxable().setup();
67 | private static final InheritanceAccessBoxable INHERITANCE_ACCESS_BOXABLE = new InheritanceAccessBoxable().setup();
68 | private static final InheritanceMultipleBoxable INHERITANCE_MULTIPLE_BOXABLE = new InheritanceMultipleBoxable().setup();
69 | private static final ObjectBoxable OBJECT_BOXABLE = new ObjectBoxable().setup();
70 | private static final EnumBoxable ENUM_BOXABLE = new EnumBoxable().setup();
71 | private static final AdapterBoxable ADAPTER_BOXABLE = new AdapterBoxable().setup();
72 | private static final SerializeBoxable SERIALIZE_BOXABLE = new SerializeBoxable().setup();
73 | private static final NestedBoxable NESTED_BOXABLE = new NestedBoxable().setup();
74 |
75 | public abstract Boxer> getBoxer();
76 |
77 | @Before
78 | public void setup() {
79 | boxer = getBoxer();
80 | before();
81 | }
82 |
83 | public void before() {
84 | //Empty implementation
85 | }
86 |
87 | @After
88 | public void after() {
89 | //Empty implementation
90 | }
91 |
92 | public void between() {
93 | //Empty implementation
94 | }
95 |
96 | @Test
97 | public void adapter() {
98 | Assertions.assertThat(boxer.get(KEY, Date.class))
99 | .isNull();
100 | between();
101 | boxer.add(KEY, DATE_ADAPTER);
102 | between();
103 | Assertions.assertThat(boxer.get(KEY, Date.class))
104 | .isEqualTo(DATE_ADAPTER);
105 | }
106 |
107 | @Test
108 | public void adapterArray() {
109 | Assertions.assertThat(boxer.getArray(KEY, Date.class))
110 | .isNull();
111 | between();
112 | boxer.addArray(KEY, DATE_ADAPTER_ARRAY);
113 | between();
114 | Assertions.assertThat(boxer.getArray(KEY, Date.class))
115 | .isEqualTo(DATE_ADAPTER_ARRAY);
116 | }
117 |
118 | @Test
119 | public void adapterList() {
120 | Assertions.assertThat(boxer.getList(KEY, Date.class, ArrayList.class))
121 | .isNull();
122 | between();
123 | boxer.addList(KEY, Arrays.asList(DATE_ADAPTER_ARRAY));
124 | between();
125 | Assertions.assertThat(boxer.getList(KEY, Date.class, ArrayList.class))
126 | .isEqualTo(Arrays.asList(DATE_ADAPTER_ARRAY));
127 | }
128 |
129 | @Test
130 | public void boxable() {
131 | Assertions.assertThat(boxer.get(KEY, PrimaryBoxable.class))
132 | .isNull();
133 | between();
134 | boxer.add(KEY, BOXABLE);
135 | between();
136 | Assertions.assertThat(boxer.get(KEY, PrimaryBoxable.class))
137 | .isEqualTo(BOXABLE);
138 | }
139 |
140 | @Test
141 | public void boxableArray() {
142 | Assertions.assertThat(boxer.getArray(KEY, PrimaryBoxable.class))
143 | .isNull();
144 | between();
145 | boxer.addArray(KEY, BOXABLE_ARRAY);
146 | between();
147 | Assertions.assertThat(boxer.getArray(KEY, PrimaryBoxable.class))
148 | .isEqualTo(BOXABLE_ARRAY);
149 | }
150 |
151 | @Test
152 | public void boxableList() {
153 | Assertions.assertThat(boxer.getList(KEY, PrimaryBoxable.class, ArrayList.class))
154 | .isNull();
155 | between();
156 | boxer.addList(KEY, Arrays.asList(BOXABLE_ARRAY));
157 | between();
158 | Assertions.assertThat(boxer.getList(KEY, PrimaryBoxable.class, ArrayList.class))
159 | .isEqualTo(Arrays.asList(BOXABLE_ARRAY));
160 | }
161 |
162 | @Test
163 | public void Enum() {
164 | Assertions.assertThat(boxer.getEnum(KEY, PrimaryEnum.class))
165 | .isNull();
166 | between();
167 | boxer.addEnum(KEY, ENUM);
168 | between();
169 | Assertions.assertThat(boxer.getEnum(KEY, PrimaryEnum.class))
170 | .isEqualTo(ENUM);
171 | }
172 |
173 | @Test
174 | public void enumArray() {
175 | Assertions.assertThat(boxer.getEnumArray(KEY, PrimaryEnum.class))
176 | .isNull();
177 | between();
178 | boxer.addEnumArray(KEY, ENUM_ARRAY);
179 | between();
180 | Assertions.assertThat(boxer.getEnumArray(KEY, PrimaryEnum.class))
181 | .isEqualTo(ENUM_ARRAY);
182 | }
183 |
184 | @Test
185 | public void enumList() {
186 | Assertions.assertThat(boxer.getEnumList(KEY, PrimaryEnum.class, ArrayList.class))
187 | .isNull();
188 | between();
189 | boxer.addEnumList(KEY, Arrays.asList(ENUM_ARRAY));
190 | between();
191 | Assertions.assertThat(boxer.getEnumList(KEY, PrimaryEnum.class, ArrayList.class))
192 | .isEqualTo(Arrays.asList(ENUM_ARRAY));
193 | }
194 |
195 | @Test
196 | public void string() {
197 | Assertions.assertThat(boxer.getString(KEY))
198 | .isNull();
199 | between();
200 | boxer.addString(KEY, STRING);
201 | between();
202 | Assertions.assertThat(boxer.getString(KEY))
203 | .isEqualTo(STRING);
204 | }
205 |
206 | @Test
207 | public void stringArray() {
208 | Assertions.assertThat(boxer.getStringArray(KEY))
209 | .isNull();
210 | between();
211 | boxer.addStringArray(KEY, STRING_ARRAY);
212 | between();
213 | Assertions.assertThat(boxer.getStringArray(KEY))
214 | .isEqualTo(STRING_ARRAY);
215 | }
216 |
217 | @Test
218 | public void stringList() {
219 | Assertions.assertThat(boxer.getStringList(KEY, ArrayList.class))
220 | .isNull();
221 | between();
222 | boxer.addStringList(KEY, Arrays.asList(STRING_ARRAY));
223 | between();
224 | Assertions.assertThat(boxer.getStringList(KEY, ArrayList.class))
225 | .isEqualTo(Arrays.asList(STRING_ARRAY));
226 | }
227 |
228 | @Test
229 | public void Boolean() {
230 | Assertions.assertThat(boxer.getBoolean(KEY))
231 | .isFalse();
232 | between();
233 | boxer.addBoolean(KEY, BOOLEAN);
234 | between();
235 | Assertions.assertThat(boxer.getBoolean(KEY))
236 | .isEqualTo(BOOLEAN);
237 | }
238 |
239 | @Test
240 | public void booleanArray() {
241 | Assertions.assertThat(boxer.getBooleanArray(KEY))
242 | .isNull();
243 | between();
244 | boxer.addBooleanArray(KEY, BOOLEAN_ARRAY);
245 | between();
246 | Assertions.assertThat(boxer.getBooleanArray(KEY))
247 | .isEqualTo(BOOLEAN_ARRAY);
248 | }
249 |
250 | @Test
251 | public void booleanList() {
252 | Assertions.assertThat(boxer.getBooleanList(KEY, ArrayList.class))
253 | .isNull();
254 | between();
255 | boxer.addBooleanList(KEY, Booleans.asList(BOOLEAN_ARRAY));
256 | between();
257 | Assertions.assertThat(boxer.getBooleanList(KEY, ArrayList.class))
258 | .isEqualTo(Booleans.asList(BOOLEAN_ARRAY));
259 | }
260 |
261 | @Test
262 | public void Byte() {
263 | Assertions.assertThat(boxer.getByte(KEY))
264 | .isZero();
265 | between();
266 | boxer.addByte(KEY, BYTE);
267 | between();
268 | Assertions.assertThat(boxer.getByte(KEY))
269 | .isEqualTo(BYTE);
270 | }
271 |
272 | @Test
273 | public void byteArray() {
274 | Assertions.assertThat(boxer.getByteArray(KEY))
275 | .isNull();
276 | between();
277 | boxer.addByteArray(KEY, BYTE_ARRAY);
278 | between();
279 | Assertions.assertThat(boxer.getByteArray(KEY))
280 | .isEqualTo(BYTE_ARRAY);
281 | }
282 |
283 | @Test
284 | public void byteList() {
285 | Assertions.assertThat(boxer.getByteList(KEY, ArrayList.class))
286 | .isNull();
287 | between();
288 | boxer.addByteList(KEY, Bytes.asList(BYTE_ARRAY));
289 | between();
290 | Assertions.assertThat(boxer.getByteList(KEY, ArrayList.class))
291 | .isEqualTo(Bytes.asList(BYTE_ARRAY));
292 | }
293 |
294 | @Test
295 | public void character() {
296 | //Work around because of weird behaviour of AssertJ
297 | assert boxer.getChar(KEY) == 0;
298 |
299 | between();
300 | boxer.addChar(KEY, CHARACTER);
301 | between();
302 | Assertions.assertThat(boxer.getChar(KEY))
303 | .isEqualTo(CHARACTER);
304 | }
305 |
306 | @Test
307 | public void characterArray() {
308 | Assertions.assertThat(boxer.getCharArray(KEY))
309 | .isNull();
310 | between();
311 | boxer.addCharArray(KEY, CHARACTER_ARRAY);
312 | between();
313 | Assertions.assertThat(boxer.getCharArray(KEY))
314 | .isEqualTo(CHARACTER_ARRAY);
315 | }
316 |
317 | @Test
318 | public void characterList() {
319 | Assertions.assertThat(boxer.getCharList(KEY, ArrayList.class))
320 | .isNull();
321 | between();
322 | boxer.addCharList(KEY, Chars.asList(CHARACTER_ARRAY));
323 | between();
324 | Assertions.assertThat(boxer.getCharList(KEY, ArrayList.class))
325 | .isEqualTo(Chars.asList(CHARACTER_ARRAY));
326 | }
327 |
328 | @Test
329 | public void Short() {
330 | Assertions.assertThat(boxer.getShort(KEY))
331 | .isZero();
332 | between();
333 | boxer.addShort(KEY, SHORT);
334 | between();
335 | Assertions.assertThat(boxer.getShort(KEY))
336 | .isEqualTo(SHORT);
337 | }
338 |
339 | @Test
340 | public void shortArray() {
341 | Assertions.assertThat(boxer.getShortArray(KEY))
342 | .isNull();
343 | between();
344 | boxer.addShortArray(KEY, SHORT_ARRAY);
345 | between();
346 | Assertions.assertThat(boxer.getShortArray(KEY))
347 | .isEqualTo(SHORT_ARRAY);
348 | }
349 |
350 | @Test
351 | public void shortList() {
352 | Assertions.assertThat(boxer.getShortList(KEY, ArrayList.class))
353 | .isNull();
354 | between();
355 | boxer.addShortList(KEY, Shorts.asList(SHORT_ARRAY));
356 | between();
357 | Assertions.assertThat(boxer.getShortList(KEY, ArrayList.class))
358 | .isEqualTo(Shorts.asList(SHORT_ARRAY));
359 | }
360 |
361 | @Test
362 | public void integer() {
363 | Assertions.assertThat(boxer.getInt(KEY))
364 | .isZero();
365 | between();
366 | boxer.addInt(KEY, INTEGER);
367 | between();
368 | Assertions.assertThat(boxer.getInt(KEY))
369 | .isEqualTo(INTEGER);
370 | }
371 |
372 | @Test
373 | public void integerArray() {
374 | Assertions.assertThat(boxer.getIntArray(KEY))
375 | .isNull();
376 | between();
377 | boxer.addIntArray(KEY, INTEGER_ARRAY);
378 | between();
379 | Assertions.assertThat(boxer.getIntArray(KEY))
380 | .isEqualTo(INTEGER_ARRAY);
381 | }
382 |
383 | @Test
384 | public void integerList() {
385 | Assertions.assertThat(boxer.getIntList(KEY, ArrayList.class))
386 | .isNull();
387 | between();
388 | boxer.addIntList(KEY, Ints.asList(INTEGER_ARRAY));
389 | between();
390 | Assertions.assertThat(boxer.getIntList(KEY, ArrayList.class))
391 | .isEqualTo(Ints.asList(INTEGER_ARRAY));
392 | }
393 |
394 | @Test
395 | public void Long() {
396 | Assertions.assertThat(boxer.getLong(KEY))
397 | .isZero();
398 | between();
399 | boxer.addLong(KEY, LONG);
400 | between();
401 | Assertions.assertThat(boxer.getLong(KEY))
402 | .isEqualTo(LONG);
403 | }
404 |
405 | @Test
406 | public void longArray() {
407 | Assertions.assertThat(boxer.getLongArray(KEY))
408 | .isNull();
409 | between();
410 | boxer.addLongArray(KEY, LONG_ARRAY);
411 | between();
412 | Assertions.assertThat(boxer.getLongArray(KEY))
413 | .isEqualTo(LONG_ARRAY);
414 | }
415 |
416 | @Test
417 | public void longList() {
418 | Assertions.assertThat(boxer.getLongList(KEY, ArrayList.class))
419 | .isNull();
420 | between();
421 | boxer.addLongList(KEY, Longs.asList(LONG_ARRAY));
422 | between();
423 | Assertions.assertThat(boxer.getLongList(KEY, ArrayList.class))
424 | .isEqualTo(Longs.asList(LONG_ARRAY));
425 | }
426 |
427 | @Test
428 | public void Double() {
429 | Assertions.assertThat(boxer.getDouble(KEY))
430 | .isZero();
431 | between();
432 | boxer.addDouble(KEY, DOUBLE);
433 | between();
434 | Assertions.assertThat(boxer.getDouble(KEY))
435 | .isEqualTo(DOUBLE);
436 | }
437 |
438 | @Test
439 | public void doubleArray() {
440 | Assertions.assertThat(boxer.getDoubleArray(KEY))
441 | .isNull();
442 | between();
443 | boxer.addDoubleArray(KEY, DOUBLE_ARRAY);
444 | between();
445 | Assertions.assertThat(boxer.getDoubleArray(KEY))
446 | .isEqualTo(DOUBLE_ARRAY);
447 | }
448 |
449 | @Test
450 | public void doubleList() {
451 | Assertions.assertThat(boxer.getDoubleList(KEY, ArrayList.class))
452 | .isNull();
453 | between();
454 | boxer.addDoubleList(KEY, Doubles.asList(DOUBLE_ARRAY));
455 | between();
456 | Assertions.assertThat(boxer.getDoubleList(KEY, ArrayList.class))
457 | .isEqualTo(Doubles.asList(DOUBLE_ARRAY));
458 | }
459 |
460 | @Test
461 | public void Float() {
462 | Assertions.assertThat(boxer.getFloat(KEY))
463 | .isZero();
464 | between();
465 | boxer.addFloat(KEY, FLOAT);
466 | between();
467 | Assertions.assertThat(boxer.getFloat(KEY))
468 | .isEqualTo(FLOAT);
469 | }
470 |
471 | @Test
472 | public void floatArray() {
473 | Assertions.assertThat(boxer.getFloatArray(KEY))
474 | .isNull();
475 | between();
476 | boxer.addFloatArray(KEY, FLOAT_ARRAY);
477 | between();
478 | Assertions.assertThat(boxer.getFloatArray(KEY))
479 | .isEqualTo(FLOAT_ARRAY);
480 | }
481 |
482 | @Test
483 | public void floatList() {
484 | Assertions.assertThat(boxer.getFloatList(KEY, ArrayList.class))
485 | .isNull();
486 | between();
487 | boxer.addFloatList(KEY, Floats.asList(FLOAT_ARRAY));
488 | between();
489 | Assertions.assertThat(boxer.getFloatList(KEY, ArrayList.class))
490 | .isEqualTo(Floats.asList(FLOAT_ARRAY));
491 | }
492 |
493 | /**
494 | * Special cases
495 | */
496 |
497 | @Test
498 | public void listBoxable() {
499 | boxer.add(KEY, LIST_BOXABLE);
500 | between();
501 |
502 | ListBoxable listBoxable = boxer.get(KEY, ListBoxable.class);
503 | Assertions.assertThat(listBoxable.defaultList)
504 | .isOfAnyClassIn(ArrayList.class)
505 | .isEqualTo(LIST_BOXABLE.defaultList);
506 | Assertions.assertThat(listBoxable.arrayList)
507 | .isOfAnyClassIn(ArrayList.class)
508 | .isEqualTo(LIST_BOXABLE.arrayList);
509 | Assertions.assertThat(listBoxable.arrayWrapList)
510 | .isOfAnyClassIn(ArrayList.class)
511 | .isEqualTo(LIST_BOXABLE.arrayWrapList);
512 | Assertions.assertThat(listBoxable.stackList)
513 | .isOfAnyClassIn(Stack.class)
514 | .isEqualTo(LIST_BOXABLE.stackList);
515 | Assertions.assertThat(listBoxable.stackWrapList)
516 | .isOfAnyClassIn(Stack.class)
517 | .isEqualTo(LIST_BOXABLE.stackWrapList);
518 | Assertions.assertThat(listBoxable.linkedList)
519 | .isOfAnyClassIn(LinkedList.class)
520 | .isEqualTo(LIST_BOXABLE.linkedList);
521 | Assertions.assertThat(listBoxable.linkedWrapList)
522 | .isOfAnyClassIn(LinkedList.class)
523 | .isEqualTo(LIST_BOXABLE.linkedWrapList);
524 | }
525 |
526 | @Test
527 | public void transientBoxable() {
528 | boxer.add(KEY, TRANSIENT_BOXABLE);
529 | between();
530 | Assertions.assertThat(boxer.get(KEY, TransientBoxable.class))
531 | .isNotEqualTo(TRANSIENT_BOXABLE);
532 | }
533 |
534 | @Test
535 | public void AccessModifiers() {
536 | boxer.add(KEY, ACCESS_BOXABLE);
537 | between();
538 | Assertions.assertThat(boxer.get(KEY, AccessBoxable.class))
539 | .isEqualTo(ACCESS_BOXABLE);
540 | }
541 |
542 | @Test
543 | public void inheritencePrimaryBoxable() {
544 | boxer.add(KEY, INHERITANCE_PRIMARY_BOXABLE);
545 | between();
546 | Assertions.assertThat(boxer.get(KEY, InheritancePrimaryBoxable.class))
547 | .isEqualTo(INHERITANCE_PRIMARY_BOXABLE);
548 | }
549 |
550 | @Test
551 | public void inheritenceAccessBoxable() {
552 | boxer.add(KEY, INHERITANCE_ACCESS_BOXABLE);
553 | between();
554 | Assertions.assertThat(boxer.get(KEY, InheritanceAccessBoxable.class))
555 | .isEqualTo(INHERITANCE_ACCESS_BOXABLE);
556 | }
557 |
558 | @Test
559 | public void inheritenceMultipleBoxable() {
560 | boxer.add(KEY, INHERITANCE_MULTIPLE_BOXABLE);
561 | between();
562 | Assertions.assertThat(boxer.get(KEY, InheritanceMultipleBoxable.class))
563 | .isEqualTo(INHERITANCE_MULTIPLE_BOXABLE);
564 | }
565 |
566 | @Test
567 | public void objectBoxable() {
568 | boxer.add(KEY, OBJECT_BOXABLE);
569 | between();
570 | Assertions.assertThat(boxer.get(KEY, ObjectBoxable.class))
571 | .isEqualTo(OBJECT_BOXABLE);
572 | }
573 |
574 | @Test
575 | public void enumBoxable() {
576 | boxer.add(KEY, ENUM_BOXABLE);
577 | between();
578 | Assertions.assertThat(boxer.get(KEY, EnumBoxable.class))
579 | .isEqualTo(ENUM_BOXABLE);
580 | }
581 |
582 | @Test
583 | public void replaceBoxable() {
584 | boxer.add(KEY, BOXABLE);
585 | between();
586 | boxer.add(KEY, new PrimaryBoxable());
587 | between();
588 | Assertions.assertThat(boxer.get(KEY, PrimaryBoxable.class))
589 | .isEqualTo(new PrimaryBoxable());
590 | }
591 |
592 | @Test
593 | public void replaceList() {
594 | boxer.addIntList(KEY, Arrays.asList(1, 2, 3, 4, 5));
595 | between();
596 | boxer.addIntList(KEY, Ints.asList(INTEGER_ARRAY));
597 | between();
598 | Assertions.assertThat(boxer.getIntList(KEY, ArrayList.class))
599 | .isEqualTo(Ints.asList(INTEGER_ARRAY));
600 | }
601 |
602 | @Test
603 | public void replaceArray() {
604 | boxer.addIntArray(KEY, new int[]{1, 2, 3, 4, 5});
605 | between();
606 | boxer.addIntArray(KEY, INTEGER_ARRAY);
607 | between();
608 | Assertions.assertThat(boxer.getIntArray(KEY))
609 | .isEqualTo(INTEGER_ARRAY);
610 | }
611 |
612 | @Test
613 | public void adapterBoxable() {
614 | boxer.add(KEY, ADAPTER_BOXABLE);
615 | between();
616 | Assertions.assertThat(boxer.get(KEY, AdapterBoxable.class))
617 | .isEqualTo(ADAPTER_BOXABLE);
618 | }
619 |
620 | @Test
621 | public void serializeBoxable() {
622 | boxer.add(KEY, SERIALIZE_BOXABLE);
623 | between();
624 | Assertions.assertThat(boxer.get(KEY, SerializeBoxable.class))
625 | .isEqualTo(SERIALIZE_BOXABLE);
626 | }
627 |
628 | @Test
629 | public void NestedBoxable() {
630 | boxer.add(KEY, NESTED_BOXABLE);
631 | between();
632 | Assertions.assertThat(boxer.get(KEY, NestedBoxable.class))
633 | .isEqualTo(NESTED_BOXABLE);
634 | }
635 | }
636 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/BoxerTest.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer;
2 |
3 | import android.os.Bundle;
4 | import android.os.Parcel;
5 | import com.google.android.gms.wearable.DataMap;
6 | import com.larswerkman.boxer.boxables.InheritancePrimaryBoxable;
7 | import com.larswerkman.boxer.boxables.PrimaryBoxable;
8 | import com.larswerkman.boxer.boxables.TransientBoxable;
9 | import com.larswerkman.boxer.wrappers.TestWrapper;
10 | import com.larswerkman.boxer.wrappers.android.*;
11 | import org.assertj.core.api.Assertions;
12 | import org.junit.Before;
13 | import org.junit.Test;
14 | import org.junit.runner.RunWith;
15 | import org.robolectric.RobolectricTestRunner;
16 | import org.robolectric.RuntimeEnvironment;
17 | import org.robolectric.annotation.Config;
18 |
19 | import java.util.HashMap;
20 |
21 | /**
22 | * Created by lars on 22-04-15.
23 | */
24 | @RunWith(RobolectricTestRunner.class)
25 | @Config(manifest = Config.NONE)
26 | public class BoxerTest {
27 |
28 | private SQLiteWrapperTest.SQLiteHelper sqLiteHelper = new SQLiteWrapperTest.SQLiteHelper(RuntimeEnvironment.application);
29 |
30 | @Before
31 | public void setup(){
32 | Boxer.registerWrapper(TestWrapper.class, HashMap.class);
33 | }
34 |
35 | @Test
36 | public void failObtainWrapper(){
37 | Assertions.assertThat(Boxer.from(null))
38 | .isNull();
39 | }
40 |
41 | @Test
42 | public void obtainDefaultWrappers(){
43 | Assertions.assertThat(Boxer.from(new Bundle()))
44 | .isOfAnyClassIn(BundleWrapper.class).isNotNull();
45 | Assertions.assertThat(Boxer.from(new DataMap()))
46 | .isOfAnyClassIn(DataMapWrapper.class).isNotNull();
47 | Assertions.assertThat(Boxer.from(Parcel.obtain()))
48 | .isOfAnyClassIn(ParcelWrapper.class).isNotNull();
49 | Assertions.assertThat(Boxer.from(sqLiteHelper.getReadableDatabase()))
50 | .isOfAnyClassIn(SQLiteWrapper.class).isNotNull();
51 | }
52 |
53 | @Test
54 | public void obtainTestWrapper(){
55 | Assertions.assertThat(Boxer.from(new HashMap()))
56 | .isOfAnyClassIn(TestWrapper.class).isNotNull();
57 | }
58 |
59 | @Test
60 | public void removeTestWrapper(){
61 | Assertions.assertThat(Boxer.removeWrapper(TestWrapper.class))
62 | .isSameAs(HashMap.class);
63 | Assertions.assertThat(Boxer.from(new HashMap()))
64 | .isNull();
65 | }
66 |
67 | @Test
68 | public void removeTestWrapperByType(){
69 | Assertions.assertThat(Boxer.removeWrapperForType(HashMap.class))
70 | .isSameAs(TestWrapper.class);
71 | Assertions.assertThat(Boxer.from(new HashMap()))
72 | .isNull();
73 | }
74 |
75 | @Test
76 | public void clearWrappers(){
77 | Boxer.clearWrappers();
78 | Assertions.assertThat(Boxer.from(new HashMap()))
79 | .isNull();
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/adapters/DateTypeAdapter.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.adapters;
2 |
3 | import com.larswerkman.boxer.Boxer;
4 | import com.larswerkman.boxer.TypeAdapter;
5 | import com.larswerkman.boxer.annotations.Adapter;
6 | import com.larswerkman.boxer.boxables.PrimaryBoxable;
7 |
8 | import java.util.Date;
9 |
10 | /**
11 | * Created by lars on 24-05-15.
12 | */
13 | @Adapter
14 | public class DateTypeAdapter extends TypeAdapter {
15 |
16 | private static final String TIME_KEY = "time_key";
17 |
18 | @Override
19 | public void serialize(Boxer> boxer, Date object) {
20 | boxer.addLong(TIME_KEY, object.getTime());
21 | }
22 |
23 | @Override
24 | public Date deserialize (Boxer> boxer) {
25 | return new Date(boxer.getLong(TIME_KEY));
26 | }
27 | }
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/AccessBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | /**
6 | * Created by lars on 23-04-15.
7 | */
8 | @Box
9 | public class AccessBoxable {
10 |
11 | String defaultMod;
12 | public String publicMod;
13 | private String privateMod;
14 | protected String protectedMod;
15 |
16 | public AccessBoxable setup(){
17 | defaultMod = "default";
18 | publicMod = "public";
19 | privateMod = "private";
20 | protectedMod = "protected";
21 |
22 | return this;
23 | }
24 |
25 | public void setPrivateMod(String privateMod) {
26 | this.privateMod = privateMod;
27 | }
28 |
29 | public String getPrivateMod() {
30 | return privateMod;
31 | }
32 |
33 | @Override
34 | public boolean equals(Object obj) {
35 | return !(obj == null
36 | || !(obj instanceof AccessBoxable))
37 | && ((obj == this)
38 | || (defaultMod.equals(((AccessBoxable) obj).defaultMod)
39 | && (publicMod.equals(((AccessBoxable) obj).publicMod))
40 | && (privateMod.equals(((AccessBoxable) obj).privateMod))
41 | && (protectedMod.equals(((AccessBoxable) obj).protectedMod))));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/AdapterBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | import java.util.Date;
6 |
7 | /**
8 | * Created by lars on 05-06-15.
9 | */
10 | @Box
11 | public class AdapterBoxable {
12 |
13 | public Date date;
14 |
15 | public AdapterBoxable setup(){
16 | date = new Date();
17 | return this;
18 | }
19 |
20 | @Override
21 | public boolean equals(Object obj) {
22 | return !(obj == null
23 | || !(obj instanceof AdapterBoxable))
24 | && ((obj == this)
25 | || (date.equals(((AdapterBoxable) obj).date)));
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/EnumBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 | import com.larswerkman.boxer.enums.PrimaryEnum;
5 |
6 | import java.util.Arrays;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by lars on 21-05-15.
11 | */
12 | @Box
13 | public class EnumBoxable {
14 |
15 | public PrimaryEnum primaryEnum;
16 | public PrimaryEnum[] primaryEnumArray;
17 | public List primaryEnumList;
18 |
19 | public EnumBoxable setup(){
20 | primaryEnum = PrimaryEnum.TO_BE;
21 | primaryEnumArray = PrimaryEnum.values();
22 | primaryEnumList = Arrays.asList(PrimaryEnum.values());
23 |
24 | return this;
25 | }
26 |
27 | @Override
28 | public boolean equals(Object obj) {
29 | return !(obj == null
30 | || !(obj instanceof EnumBoxable))
31 | && ((obj == this)
32 | || (primaryEnum.equals(((EnumBoxable) obj).primaryEnum)
33 | && primaryEnumList.equals(((EnumBoxable) obj).primaryEnumList)
34 | && Arrays.deepEquals(primaryEnumArray, ((EnumBoxable) obj).primaryEnumArray)));
35 | }
36 | }
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/InheritanceAccessBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | /**
6 | * Created by lars on 28-04-15.
7 | */
8 | @Box
9 | public class InheritanceAccessBoxable extends AccessBoxable {
10 |
11 | public String inheritance;
12 |
13 | public InheritanceAccessBoxable setup(){
14 | super.setup();
15 | inheritance = "Inheritance";
16 | return this;
17 | }
18 |
19 | @Override
20 | public boolean equals(Object obj) {
21 | return !(obj == null
22 | || !(obj instanceof InheritanceAccessBoxable))
23 | && ((obj == this)
24 | || (super.equals(obj)
25 | && inheritance.equals(((InheritanceAccessBoxable) obj).inheritance)));
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/InheritanceMultipleBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | /**
6 | * Created by lars on 28-04-15.
7 | */
8 | @Box
9 | public class InheritanceMultipleBoxable extends InheritanceAccessBoxable {
10 |
11 | public String multiple;
12 |
13 | public InheritanceMultipleBoxable setup(){
14 | super.setup();
15 | multiple = "Multiple";
16 | return this;
17 | }
18 |
19 | @Override
20 | public boolean equals(Object obj) {
21 | return !(obj == null
22 | || !(obj instanceof InheritanceMultipleBoxable))
23 | && ((obj == this)
24 | || (super.equals(obj)
25 | && multiple.equals(((InheritanceMultipleBoxable) obj).multiple)));
26 | }
27 | }
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/InheritancePrimaryBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | /**
6 | * Created by lars on 28-04-15.
7 | */
8 | @Box
9 | public class InheritancePrimaryBoxable extends PrimaryBoxable {
10 |
11 | public String inheritance;
12 |
13 | public InheritancePrimaryBoxable setup(){
14 | super.setup();
15 | inheritance = "Inheritance";
16 | return this;
17 | }
18 |
19 | @Override
20 | public boolean equals(Object obj) {
21 | return !(obj == null
22 | || !(obj instanceof InheritancePrimaryBoxable))
23 | && ((obj == this)
24 | || (super.equals(obj)
25 | && inheritance.equals(((InheritancePrimaryBoxable) obj).inheritance)));
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/ListBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 | import com.larswerkman.boxer.annotations.Wrap;
5 |
6 | import java.util.*;
7 |
8 | /**
9 | * Created by lars on 23-04-15.
10 | */
11 | @Box
12 | public class ListBoxable {
13 |
14 | public List defaultList;
15 |
16 | public ArrayList arrayList;
17 | @Wrap(ArrayList.class)
18 | public List arrayWrapList;
19 |
20 | public Stack stackList;
21 | @Wrap(Stack.class)
22 | public List stackWrapList;
23 |
24 | public LinkedList linkedList;
25 | @Wrap(LinkedList.class)
26 | public List linkedWrapList;
27 |
28 | public ListBoxable setup(){
29 | defaultList = new ArrayList();
30 | defaultList.add(1);
31 | defaultList.add(2);
32 | defaultList.add(3);
33 | defaultList.add(4);
34 | defaultList.add(5);
35 |
36 | arrayList = new ArrayList();
37 | arrayList.addAll(defaultList);
38 |
39 | arrayWrapList = new ArrayList();
40 | arrayWrapList.addAll(defaultList);
41 |
42 | stackList = new Stack();
43 | stackList.addAll(defaultList);
44 |
45 | stackWrapList = new Stack();
46 | stackWrapList.addAll(defaultList);
47 |
48 | linkedList = new LinkedList();
49 | linkedList.addAll(defaultList);
50 |
51 | linkedWrapList = new LinkedList();
52 | linkedWrapList.addAll(defaultList);
53 |
54 | return this;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/NestedBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | /**
6 | * Created by lars on 12-09-15.
7 | */
8 | @Box
9 | public class NestedBoxable {
10 |
11 | @Box
12 | public static class StaticFirst {
13 |
14 | @Box
15 | public static class StaticSecond {
16 | public String string;
17 |
18 | public StaticSecond setup() {
19 | string = "string";
20 | return this;
21 | }
22 |
23 | @Override
24 | public boolean equals(Object obj) {
25 | return !(obj == null
26 | || !(obj instanceof StaticSecond))
27 | && ((obj == this)
28 | || string.equals(((StaticSecond) obj).string));
29 | }
30 | }
31 |
32 | public StaticSecond second;
33 |
34 | public StaticFirst setup() {
35 | second = new StaticSecond().setup();
36 | return this;
37 | }
38 |
39 | @Override
40 | public boolean equals(Object obj) {
41 | return !(obj == null
42 | || !(obj instanceof StaticFirst))
43 | && ((obj == this)
44 | || second.equals(((StaticFirst) obj).second));
45 | }
46 | }
47 |
48 | public StaticFirst staticFirst;
49 |
50 | public NestedBoxable setup() {
51 | staticFirst = new StaticFirst().setup();
52 | return this;
53 | }
54 |
55 | @Override
56 | public boolean equals(Object obj) {
57 | return !(obj == null
58 | || !(obj instanceof NestedBoxable))
59 | && ((obj == this)
60 | || staticFirst.equals(((NestedBoxable) obj).staticFirst));
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/ObjectBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | import java.util.Arrays;
6 | import java.util.List;
7 |
8 | /**
9 | * Created by lars on 29-04-15.
10 | */
11 | @Box
12 | public class ObjectBoxable {
13 |
14 | public PrimaryBoxable primaryBoxable;
15 | public PrimaryBoxable[] primaryBoxableArray;
16 | public List primaryBoxableList;
17 |
18 | public ObjectBoxable setup(){
19 | primaryBoxable = new PrimaryBoxable().setup();
20 | primaryBoxableArray = new PrimaryBoxable[]{
21 | new PrimaryBoxable().setup(),
22 | new PrimaryBoxable().setup()
23 | };
24 | primaryBoxableList = Arrays.asList(
25 | new PrimaryBoxable().setup(),
26 | new PrimaryBoxable().setup()
27 | );
28 |
29 | return this;
30 | }
31 |
32 | @Override
33 | public boolean equals(Object obj) {
34 | return !(obj == null
35 | || !(obj instanceof ObjectBoxable))
36 | && ((obj == this)
37 | || (primaryBoxable.equals(((ObjectBoxable) obj).primaryBoxable)
38 | && primaryBoxableList.equals(((ObjectBoxable) obj).primaryBoxableList)
39 | && Arrays.deepEquals(primaryBoxableArray, ((ObjectBoxable) obj).primaryBoxableArray)));
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/PrimaryBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | /**
6 | * Created by lars on 23-04-15.
7 | */
8 | @Box
9 | public class PrimaryBoxable {
10 |
11 | public String aString = "";
12 | public boolean aBoolean;
13 | public byte aByte;
14 | public char aChar;
15 | public short aShort;
16 | public int anInt;
17 | public long aLong;
18 | public double aDouble;
19 | public float aFloat;
20 |
21 | public PrimaryBoxable setup(){
22 | aString = "string";
23 | aBoolean = true;
24 | aByte = 0xA;
25 | aChar = 'A';
26 | aShort = 99;
27 | anInt = 99;
28 | aLong = 99l;
29 | aDouble = 0.99;
30 | aFloat = 0.99f;
31 |
32 | return this;
33 | }
34 |
35 | @Override
36 | public boolean equals(Object obj) {
37 | return !(obj == null
38 | || !(obj instanceof PrimaryBoxable))
39 | && ((obj == this)
40 | || (aString.equals(((PrimaryBoxable) obj).aString)
41 | && aBoolean == ((PrimaryBoxable) obj).aBoolean
42 | && aByte == ((PrimaryBoxable) obj).aByte
43 | && aChar == ((PrimaryBoxable) obj).aChar
44 | && aShort == ((PrimaryBoxable) obj).aShort
45 | && anInt == ((PrimaryBoxable) obj).anInt
46 | && aLong == ((PrimaryBoxable) obj).aLong
47 | && aDouble == ((PrimaryBoxable) obj).aDouble
48 | && aFloat == ((PrimaryBoxable) obj).aFloat));
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/SerializeBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.Boxer;
4 | import com.larswerkman.boxer.Execution;
5 | import com.larswerkman.boxer.annotations.Box;
6 | import com.larswerkman.boxer.annotations.Deserialize;
7 | import com.larswerkman.boxer.annotations.Serialize;
8 | import org.assertj.core.api.Assertions;
9 |
10 | /**
11 | * Created by lars on 25-05-15.
12 | */
13 | @Box
14 | public class SerializeBoxable {
15 |
16 | private transient boolean serialized = false;
17 | private transient boolean deserialized = false;
18 |
19 | private transient String aString;
20 |
21 | public SerializeBoxable setup(){
22 | aString = "String";
23 |
24 | return this;
25 | }
26 |
27 | @Serialize(Execution.BEFORE)
28 | public void beforeSerialization(Boxer> boxer){
29 | if(serialized){
30 | Assertions.fail("@Serialize(BEFORE) should be called before (AFTER)");
31 | }
32 | serialized = true;
33 |
34 | boxer.addString("KEY", aString);
35 | }
36 |
37 | @Serialize(Execution.AFTER)
38 | public void afterSerialization(Boxer> boxer) {
39 | if(!serialized){
40 | Assertions.fail("@Serialize(AFTER) should be called after (BEFORE)");
41 | }
42 | serialized = false;
43 | }
44 |
45 | @Deserialize(Execution.BEFORE)
46 | public void beforeDeserialization(Boxer> boxer) {
47 | if(deserialized){
48 | Assertions.fail("@Deserialize(BEFORE) should be called before (AFTER)");
49 | }
50 | deserialized = true;
51 | }
52 |
53 | @Deserialize(Execution.AFTER)
54 | public void afterDeserialization(Boxer> boxer){
55 | if(!deserialized){
56 | Assertions.fail("@Deserialize(AFTER) should be called after (BEFORE)");
57 | }
58 | deserialized = false;
59 |
60 | aString = boxer.getString("KEY");
61 | }
62 |
63 | @Serialize
64 | public void noArgument(){
65 |
66 | }
67 |
68 | @Serialize
69 | public void noWildCardArgument(Boxer boxer){
70 |
71 | }
72 |
73 | @Override
74 | public boolean equals(Object obj) {
75 | return !(obj == null
76 | || !(obj instanceof SerializeBoxable))
77 | && ((obj == this)
78 | || (aString.equals(((SerializeBoxable) obj).aString)));
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/boxables/TransientBoxable.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.boxables;
2 |
3 | import com.larswerkman.boxer.annotations.Box;
4 |
5 | /**
6 | * Created by lars on 23-04-15.
7 | */
8 | @Box
9 | public class TransientBoxable {
10 |
11 | public transient String string;
12 | public transient int integer;
13 |
14 | public TransientBoxable setup(){
15 | string = "String";
16 | integer = 99;
17 |
18 | return this;
19 | }
20 |
21 | @Override
22 | public boolean equals(Object obj) {
23 | return !(obj == null
24 | || !(obj instanceof TransientBoxable))
25 | && ((obj == this)
26 | || (string != null
27 | && string.equals(((TransientBoxable) obj).string)
28 | && integer == ((TransientBoxable) obj).integer));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/enums/PrimaryEnum.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.enums;
2 |
3 | /**
4 | * Created by lars on 23-04-15.
5 | */
6 | public enum PrimaryEnum {
7 | TO_BE, NOT_TO_BE
8 | }
9 |
--------------------------------------------------------------------------------
/boxer/src/test/java/com/larswerkman/boxer/wrappers/TestWrapper.java:
--------------------------------------------------------------------------------
1 | package com.larswerkman.boxer.wrappers;
2 |
3 | import com.larswerkman.boxer.Boxer;
4 |
5 | import java.lang.reflect.Array;
6 | import java.util.HashMap;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by lars on 23-04-15.
11 | */
12 | public class TestWrapper extends Boxer> {
13 |
14 | private HashMap map;
15 |
16 | /**
17 | * Empty constructor, Can't be a generic type because of ClassNotFoundException
18 | *
19 | * @param map Serialization object
20 | */
21 | public TestWrapper(HashMap map) {
22 | super(map);
23 | this.map = map;
24 | }
25 |
26 | @Override
27 | public void add(String key, Object value) {
28 | map.put(key, serialize(new TestWrapper(new HashMap()), value));
29 | }
30 |
31 | @Override
32 | public void addArray(String key, Object[] value) {
33 | HashMap boxeables = new HashMap();
34 | boxeables.put("size", value.length);
35 | for(int i = 0; i < value.length; i++){
36 | boxeables.put(String.valueOf(i), serialize(new TestWrapper(new HashMap()), value[i]));
37 | }
38 | map.put(key, boxeables);
39 | }
40 |
41 | @Override
42 | public void addList(String key, List> value) {
43 | addArray(key, value.toArray());
44 | }
45 |
46 | @Override
47 | public void addEnum(String key, Enum value) {
48 | map.put(key, value);
49 | }
50 |
51 | @Override
52 | public void addEnumArray(String key, Enum[] value) {
53 | map.put(key, value);
54 | }
55 |
56 | @Override
57 | public void addEnumList(String key, List extends Enum> value) {
58 | map.put(key, value);
59 | }
60 |
61 | @Override
62 | public void addString(String key, String value) {
63 | map.put(key, value);
64 | }
65 |
66 | @Override
67 | public void addStringArray(String key, String[] value) {
68 | map.put(key, value);
69 | }
70 |
71 | @Override
72 | public void addStringList(String key, List value) {
73 | map.put(key, value);
74 | }
75 |
76 | @Override
77 | public void addBoolean(String key, boolean value) {
78 | map.put(key, value);
79 | }
80 |
81 | @Override
82 | public void addBooleanArray(String key, boolean[] value) {
83 | map.put(key, value);
84 | }
85 |
86 | @Override
87 | public void addBooleanList(String key, List value) {
88 | map.put(key, value);
89 | }
90 |
91 | @Override
92 | public void addByte(String key, byte value) {
93 | map.put(key, value);
94 | }
95 |
96 | @Override
97 | public void addByteArray(String key, byte[] value) {
98 | map.put(key, value);
99 | }
100 |
101 | @Override
102 | public void addByteList(String key, List value) {
103 | map.put(key, value);
104 | }
105 |
106 | @Override
107 | public void addChar(String key, char value) {
108 | map.put(key, value);
109 | }
110 |
111 | @Override
112 | public void addCharArray(String key, char[] value) {
113 | map.put(key, value);
114 | }
115 |
116 | @Override
117 | public void addCharList(String key, List value) {
118 | map.put(key, value);
119 | }
120 |
121 | @Override
122 | public void addShort(String key, short value) {
123 | map.put(key, value);
124 | }
125 |
126 | @Override
127 | public void addShortArray(String key, short[] value) {
128 | map.put(key, value);
129 | }
130 |
131 | @Override
132 | public void addShortList(String key, List value) {
133 | map.put(key, value);
134 | }
135 |
136 | @Override
137 | public void addInt(String key, int value) {
138 | map.put(key, value);
139 | }
140 |
141 | @Override
142 | public void addIntArray(String key, int[] value) {
143 | map.put(key, value);
144 | }
145 |
146 | @Override
147 | public void addIntList(String key, List value) {
148 | map.put(key, value);
149 | }
150 |
151 | @Override
152 | public void addLong(String key, long value) {
153 | map.put(key, value);
154 | }
155 |
156 | @Override
157 | public void addLongArray(String key, long[] value) {
158 | map.put(key, value);
159 | }
160 |
161 | @Override
162 | public void addLongList(String key, List value) {
163 | map.put(key, value);
164 | }
165 |
166 | @Override
167 | public void addDouble(String key, double value) {
168 | map.put(key, value);
169 | }
170 |
171 | @Override
172 | public void addDoubleArray(String key, double[] value) {
173 | map.put(key, value);
174 | }
175 |
176 | @Override
177 | public void addDoubleList(String key, List value) {
178 | map.put(key, value);
179 | }
180 |
181 | @Override
182 | public void addFloat(String key, float value) {
183 | map.put(key, value);
184 | }
185 |
186 | @Override
187 | public void addFloatArray(String key, float[] value) {
188 | map.put(key, value);
189 | }
190 |
191 | @Override
192 | public void addFloatList(String key, List value) {
193 | map.put(key, value);
194 | }
195 |
196 | @Override
197 | public T get(String key, Class clazz) {
198 | HashMap map = (HashMap) this.map.get(key);
199 | if(map == null){
200 | return null;
201 | }
202 | return deserialize(new TestWrapper(map), clazz);
203 | }
204 |
205 | @Override
206 | public T[] getArray(String key, Class