From 3025cb5ba090e19c82a73363132f148befd68a64 Mon Sep 17 00:00:00 2001 From: BoD Date: Sat, 12 Nov 2016 18:51:20 +0100 Subject: [PATCH] Fix for issue #43. --- CHANGELOG.md | 4 + README.md | 8 +- etc/sample/_config.json | 3 +- .../sample/provider/company/CompanyBean.java | 202 ++++++++++ .../sample/provider/manual/ManualBean.java | 180 +++++++++ .../sample/provider/person/PersonBean.java | 360 ++++++++++++++++++ .../provider/personteam/PersonTeamBean.java | 164 ++++++++ .../sample/provider/product/ProductBean.java | 176 +++++++++ .../serialnumber/SerialNumberBean.java | 180 +++++++++ .../sample/provider/team/TeamBean.java | 232 +++++++++++ etc/sample/app/build.gradle | 2 +- pom.xml | 2 +- .../androidcontentprovidergenerator/Main.java | 30 ++ .../model/Entity.java | 7 + .../androidcontentprovidergenerator/bean.ftl | 169 ++++++++ .../androidcontentprovidergenerator/model.ftl | 14 +- 16 files changed, 1720 insertions(+), 13 deletions(-) create mode 100644 etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyBean.java create mode 100644 etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualBean.java create mode 100644 etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonBean.java create mode 100644 etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamBean.java create mode 100644 etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductBean.java create mode 100644 etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberBean.java create mode 100644 etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamBean.java create mode 100644 src/main/resources/org/jraf/androidcontentprovidergenerator/bean.ftl diff --git a/CHANGELOG.md b/CHANGELOG.md index 864b4c9..7ab71d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Android ContentProvider Generator Changelog =========================================== +v1.11.0 (2016-11-12) +------ +- Beans generation (if new `generateBeans` boolean parameter in config is true) - fix for issue #43. + v1.10.0 (2016-10-30) ------ - Fix for issue #91. diff --git a/README.md b/README.md index 880d895..7612161 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ It takes a set of entity (a.k.a "table") definitions as the input, and generates - one `ContentValues` class per entity - one `Selection` class per entity - one `Model` interface per entity +- one `Bean` class per entity (optionally) How to use @@ -34,7 +35,8 @@ These are self-explanatory so here is an example: "databaseVersion": 1, "enableForeignKeys": true, "useAnnotations": true, - "useSupportLibrary": true + "useSupportLibrary": true, + "generateBeans": true } ``` @@ -128,7 +130,7 @@ https://github.com/BoD/android-contentprovider-generator/releases/latest ### Run the tool -`java -jar android_contentprovider_generator-1.10.0-bundle.jar -i -o ` +`java -jar android_contentprovider_generator-1.11.0-bundle.jar -i -o ` - Input folder: where to find `_config.json` and your entity json files - Output folder: where the resulting files will be generated @@ -243,7 +245,7 @@ You need maven to build this tool. `mvn package` -This will produce `android_contentprovider_generator-1.10.0-bundle.jar` in the `target` folder. +This will produce `android_contentprovider_generator-1.11.0-bundle.jar` in the `target` folder. Similar tools diff --git a/etc/sample/_config.json b/etc/sample/_config.json index 18e5eb5..9c96502 100644 --- a/etc/sample/_config.json +++ b/etc/sample/_config.json @@ -10,5 +10,6 @@ "databaseVersion": 1, "enableForeignKeys": true, "useAnnotations": true, - "useSupportLibrary": true + "useSupportLibrary": true, + "generateBeans": true } diff --git a/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyBean.java b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyBean.java new file mode 100644 index 0000000..56abe7c --- /dev/null +++ b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/company/CompanyBean.java @@ -0,0 +1,202 @@ +/* + * This source is part of the + * _____ ___ ____ + * __ / / _ \/ _ | / __/___ _______ _ + * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ + * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / + * /___/ + * repository. + * + * Copyright (C) 2012-2015 Benoit 'BoD' Lubek (BoD@JRAF.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jraf.androidcontentprovidergenerator.sample.provider.company; + +// @formatter:off +import org.jraf.androidcontentprovidergenerator.sample.provider.base.BaseModel; + +import java.util.Date; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * A commercial business. + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class CompanyBean implements CompanyModel { + private long mId; + private String mName; + private String mAddress; + private long mSerialNumberId; + + /** + * Primary key. + */ + @Override + public long getId() { + return mId; + } + + /** + * Primary key. + */ + public void setId(long id) { + mId = id; + } + + /** + * The commercial name of this company. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getName() { + return mName; + } + + /** + * The commercial name of this company. + * Must not be {@code null}. + */ + public void setName(@NonNull String name) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + mName = name; + } + + /** + * The full address of this company. + * Can be {@code null}. + */ + @Nullable + @Override + public String getAddress() { + return mAddress; + } + + /** + * The full address of this company. + * Can be {@code null}. + */ + public void setAddress(@Nullable String address) { + mAddress = address; + } + + /** + * The serial number of this company. + */ + @Override + public long getSerialNumberId() { + return mSerialNumberId; + } + + /** + * The serial number of this company. + */ + public void setSerialNumberId(long serialNumberId) { + mSerialNumberId = serialNumberId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CompanyBean bean = (CompanyBean) o; + return mId == bean.mId; + } + + @Override + public int hashCode() { + return (int) (mId ^ (mId >>> 32)); + } + + /** + * Instantiate a new CompanyBean with specified values. + */ + @NonNull + public static CompanyBean newInstance(long id, @NonNull String name, @Nullable String address, long serialNumberId) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + CompanyBean res = new CompanyBean(); + res.mId = id; + res.mName = name; + res.mAddress = address; + res.mSerialNumberId = serialNumberId; + return res; + } + + /** + * Instantiate a new CompanyBean with all the values copied from the given model. + */ + @NonNull + public static CompanyBean copy(@NonNull CompanyModel from) { + CompanyBean res = new CompanyBean(); + res.mId = from.getId(); + res.mName = from.getName(); + res.mAddress = from.getAddress(); + res.mSerialNumberId = from.getSerialNumberId(); + return res; + } + + public static class Builder { + private CompanyBean mRes = new CompanyBean(); + + /** + * Primary key. + */ + public Builder id(long id) { + mRes.mId = id; + return this; + } + + /** + * The commercial name of this company. + * Must not be {@code null}. + */ + public Builder name(@NonNull String name) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + mRes.mName = name; + return this; + } + + /** + * The full address of this company. + * Can be {@code null}. + */ + public Builder address(@Nullable String address) { + mRes.mAddress = address; + return this; + } + + /** + * The serial number of this company. + */ + public Builder serialNumberId(long serialNumberId) { + mRes.mSerialNumberId = serialNumberId; + return this; + } + + /** + * Get a new CompanyBean built with the given values. + */ + public CompanyBean build() { + if (mRes.mName == null) throw new IllegalArgumentException("name must not be null"); + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualBean.java b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualBean.java new file mode 100644 index 0000000..b168c4e --- /dev/null +++ b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/manual/ManualBean.java @@ -0,0 +1,180 @@ +/* + * This source is part of the + * _____ ___ ____ + * __ / / _ \/ _ | / __/___ _______ _ + * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ + * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / + * /___/ + * repository. + * + * Copyright (C) 2012-2015 Benoit 'BoD' Lubek (BoD@JRAF.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jraf.androidcontentprovidergenerator.sample.provider.manual; + +// @formatter:off +import org.jraf.androidcontentprovidergenerator.sample.provider.base.BaseModel; + +import java.util.Date; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * A manual related to a product. + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class ManualBean implements ManualModel { + private long mId; + private String mTitle; + private String mIsbn; + + /** + * Primary key. + */ + @Override + public long getId() { + return mId; + } + + /** + * Primary key. + */ + public void setId(long id) { + mId = id; + } + + /** + * Get the {@code title} value. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getTitle() { + return mTitle; + } + + /** + * Set the {@code title} value. + * Must not be {@code null}. + */ + public void setTitle(@NonNull String title) { + if (title == null) throw new IllegalArgumentException("title must not be null"); + mTitle = title; + } + + /** + * Get the {@code isbn} value. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getIsbn() { + return mIsbn; + } + + /** + * Set the {@code isbn} value. + * Must not be {@code null}. + */ + public void setIsbn(@NonNull String isbn) { + if (isbn == null) throw new IllegalArgumentException("isbn must not be null"); + mIsbn = isbn; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ManualBean bean = (ManualBean) o; + return mId == bean.mId; + } + + @Override + public int hashCode() { + return (int) (mId ^ (mId >>> 32)); + } + + /** + * Instantiate a new ManualBean with specified values. + */ + @NonNull + public static ManualBean newInstance(long id, @NonNull String title, @NonNull String isbn) { + if (title == null) throw new IllegalArgumentException("title must not be null"); + if (isbn == null) throw new IllegalArgumentException("isbn must not be null"); + ManualBean res = new ManualBean(); + res.mId = id; + res.mTitle = title; + res.mIsbn = isbn; + return res; + } + + /** + * Instantiate a new ManualBean with all the values copied from the given model. + */ + @NonNull + public static ManualBean copy(@NonNull ManualModel from) { + ManualBean res = new ManualBean(); + res.mId = from.getId(); + res.mTitle = from.getTitle(); + res.mIsbn = from.getIsbn(); + return res; + } + + public static class Builder { + private ManualBean mRes = new ManualBean(); + + /** + * Primary key. + */ + public Builder id(long id) { + mRes.mId = id; + return this; + } + + /** + * Set the {@code title} value. + * Must not be {@code null}. + */ + public Builder title(@NonNull String title) { + if (title == null) throw new IllegalArgumentException("title must not be null"); + mRes.mTitle = title; + return this; + } + + /** + * Set the {@code isbn} value. + * Must not be {@code null}. + */ + public Builder isbn(@NonNull String isbn) { + if (isbn == null) throw new IllegalArgumentException("isbn must not be null"); + mRes.mIsbn = isbn; + return this; + } + + /** + * Get a new ManualBean built with the given values. + */ + public ManualBean build() { + if (mRes.mTitle == null) throw new IllegalArgumentException("title must not be null"); + if (mRes.mIsbn == null) throw new IllegalArgumentException("isbn must not be null"); + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonBean.java b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonBean.java new file mode 100644 index 0000000..e7ddd04 --- /dev/null +++ b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/person/PersonBean.java @@ -0,0 +1,360 @@ +/* + * This source is part of the + * _____ ___ ____ + * __ / / _ \/ _ | / __/___ _______ _ + * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ + * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / + * /___/ + * repository. + * + * Copyright (C) 2012-2015 Benoit 'BoD' Lubek (BoD@JRAF.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jraf.androidcontentprovidergenerator.sample.provider.person; + +// @formatter:off +import org.jraf.androidcontentprovidergenerator.sample.provider.base.BaseModel; + +import java.util.Date; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * A human being which is part of a team. + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class PersonBean implements PersonModel { + private long mId; + private String mFirstName; + private String mLastName; + private int mAge; + private Date mBirthDate; + private boolean mHasBlueEyes; + private Float mHeight; + private Gender mGender; + private String mCountryCode; + + /** + * Primary key. + */ + @Override + public long getId() { + return mId; + } + + /** + * Primary key. + */ + public void setId(long id) { + mId = id; + } + + /** + * First name of this person. For instance, John. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getFirstName() { + return mFirstName; + } + + /** + * First name of this person. For instance, John. + * Must not be {@code null}. + */ + public void setFirstName(@NonNull String firstName) { + if (firstName == null) throw new IllegalArgumentException("firstName must not be null"); + mFirstName = firstName; + } + + /** + * Last name (a.k.a. Given name) of this person. For instance, Smith. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getLastName() { + return mLastName; + } + + /** + * Last name (a.k.a. Given name) of this person. For instance, Smith. + * Must not be {@code null}. + */ + public void setLastName(@NonNull String lastName) { + if (lastName == null) throw new IllegalArgumentException("lastName must not be null"); + mLastName = lastName; + } + + /** + * Get the {@code age} value. + */ + @Override + public int getAge() { + return mAge; + } + + /** + * Set the {@code age} value. + */ + public void setAge(int age) { + mAge = age; + } + + /** + * Get the {@code birth_date} value. + * Can be {@code null}. + */ + @Nullable + @Override + public Date getBirthDate() { + return mBirthDate; + } + + /** + * Set the {@code birth_date} value. + * Can be {@code null}. + */ + public void setBirthDate(@Nullable Date birthDate) { + mBirthDate = birthDate; + } + + /** + * If {@code true}, this person has blue eyes. Otherwise, this person doesn't have blue eyes. + */ + @Override + public boolean getHasBlueEyes() { + return mHasBlueEyes; + } + + /** + * If {@code true}, this person has blue eyes. Otherwise, this person doesn't have blue eyes. + */ + public void setHasBlueEyes(boolean hasBlueEyes) { + mHasBlueEyes = hasBlueEyes; + } + + /** + * Get the {@code height} value. + * Can be {@code null}. + */ + @Nullable + @Override + public Float getHeight() { + return mHeight; + } + + /** + * Set the {@code height} value. + * Can be {@code null}. + */ + public void setHeight(@Nullable Float height) { + mHeight = height; + } + + /** + * Get the {@code gender} value. + * Cannot be {@code null}. + */ + @NonNull + @Override + public Gender getGender() { + return mGender; + } + + /** + * Set the {@code gender} value. + * Must not be {@code null}. + */ + public void setGender(@NonNull Gender gender) { + if (gender == null) throw new IllegalArgumentException("gender must not be null"); + mGender = gender; + } + + /** + * Get the {@code country_code} value. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getCountryCode() { + return mCountryCode; + } + + /** + * Set the {@code country_code} value. + * Must not be {@code null}. + */ + public void setCountryCode(@NonNull String countryCode) { + if (countryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + mCountryCode = countryCode; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonBean bean = (PersonBean) o; + return mId == bean.mId; + } + + @Override + public int hashCode() { + return (int) (mId ^ (mId >>> 32)); + } + + /** + * Instantiate a new PersonBean with specified values. + */ + @NonNull + public static PersonBean newInstance(long id, @NonNull String firstName, @NonNull String lastName, int age, @Nullable Date birthDate, boolean hasBlueEyes, @Nullable Float height, @NonNull Gender gender, @NonNull String countryCode) { + if (firstName == null) throw new IllegalArgumentException("firstName must not be null"); + if (lastName == null) throw new IllegalArgumentException("lastName must not be null"); + if (gender == null) throw new IllegalArgumentException("gender must not be null"); + if (countryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + PersonBean res = new PersonBean(); + res.mId = id; + res.mFirstName = firstName; + res.mLastName = lastName; + res.mAge = age; + res.mBirthDate = birthDate; + res.mHasBlueEyes = hasBlueEyes; + res.mHeight = height; + res.mGender = gender; + res.mCountryCode = countryCode; + return res; + } + + /** + * Instantiate a new PersonBean with all the values copied from the given model. + */ + @NonNull + public static PersonBean copy(@NonNull PersonModel from) { + PersonBean res = new PersonBean(); + res.mId = from.getId(); + res.mFirstName = from.getFirstName(); + res.mLastName = from.getLastName(); + res.mAge = from.getAge(); + res.mBirthDate = from.getBirthDate(); + res.mHasBlueEyes = from.getHasBlueEyes(); + res.mHeight = from.getHeight(); + res.mGender = from.getGender(); + res.mCountryCode = from.getCountryCode(); + return res; + } + + public static class Builder { + private PersonBean mRes = new PersonBean(); + + /** + * Primary key. + */ + public Builder id(long id) { + mRes.mId = id; + return this; + } + + /** + * First name of this person. For instance, John. + * Must not be {@code null}. + */ + public Builder firstName(@NonNull String firstName) { + if (firstName == null) throw new IllegalArgumentException("firstName must not be null"); + mRes.mFirstName = firstName; + return this; + } + + /** + * Last name (a.k.a. Given name) of this person. For instance, Smith. + * Must not be {@code null}. + */ + public Builder lastName(@NonNull String lastName) { + if (lastName == null) throw new IllegalArgumentException("lastName must not be null"); + mRes.mLastName = lastName; + return this; + } + + /** + * Set the {@code age} value. + */ + public Builder age(int age) { + mRes.mAge = age; + return this; + } + + /** + * Set the {@code birth_date} value. + * Can be {@code null}. + */ + public Builder birthDate(@Nullable Date birthDate) { + mRes.mBirthDate = birthDate; + return this; + } + + /** + * If {@code true}, this person has blue eyes. Otherwise, this person doesn't have blue eyes. + */ + public Builder hasBlueEyes(boolean hasBlueEyes) { + mRes.mHasBlueEyes = hasBlueEyes; + return this; + } + + /** + * Set the {@code height} value. + * Can be {@code null}. + */ + public Builder height(@Nullable Float height) { + mRes.mHeight = height; + return this; + } + + /** + * Set the {@code gender} value. + * Must not be {@code null}. + */ + public Builder gender(@NonNull Gender gender) { + if (gender == null) throw new IllegalArgumentException("gender must not be null"); + mRes.mGender = gender; + return this; + } + + /** + * Set the {@code country_code} value. + * Must not be {@code null}. + */ + public Builder countryCode(@NonNull String countryCode) { + if (countryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + mRes.mCountryCode = countryCode; + return this; + } + + /** + * Get a new PersonBean built with the given values. + */ + public PersonBean build() { + if (mRes.mFirstName == null) throw new IllegalArgumentException("firstName must not be null"); + if (mRes.mLastName == null) throw new IllegalArgumentException("lastName must not be null"); + if (mRes.mGender == null) throw new IllegalArgumentException("gender must not be null"); + if (mRes.mCountryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamBean.java b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamBean.java new file mode 100644 index 0000000..24ecaec --- /dev/null +++ b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/personteam/PersonTeamBean.java @@ -0,0 +1,164 @@ +/* + * This source is part of the + * _____ ___ ____ + * __ / / _ \/ _ | / __/___ _______ _ + * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ + * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / + * /___/ + * repository. + * + * Copyright (C) 2012-2015 Benoit 'BoD' Lubek (BoD@JRAF.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jraf.androidcontentprovidergenerator.sample.provider.personteam; + +// @formatter:off +import org.jraf.androidcontentprovidergenerator.sample.provider.base.BaseModel; + +import java.util.Date; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * Entity joining people and teams. A team contains several people, and a person can belong to several teams. + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class PersonTeamBean implements PersonTeamModel { + private long mId; + private long mPersonId; + private long mTeamId; + + /** + * Primary key. + */ + @Override + public long getId() { + return mId; + } + + /** + * Primary key. + */ + public void setId(long id) { + mId = id; + } + + /** + * Get the {@code person_id} value. + */ + @Override + public long getPersonId() { + return mPersonId; + } + + /** + * Set the {@code person_id} value. + */ + public void setPersonId(long personId) { + mPersonId = personId; + } + + /** + * Get the {@code team_id} value. + */ + @Override + public long getTeamId() { + return mTeamId; + } + + /** + * Set the {@code team_id} value. + */ + public void setTeamId(long teamId) { + mTeamId = teamId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PersonTeamBean bean = (PersonTeamBean) o; + return mId == bean.mId; + } + + @Override + public int hashCode() { + return (int) (mId ^ (mId >>> 32)); + } + + /** + * Instantiate a new PersonTeamBean with specified values. + */ + @NonNull + public static PersonTeamBean newInstance(long id, long personId, long teamId) { + PersonTeamBean res = new PersonTeamBean(); + res.mId = id; + res.mPersonId = personId; + res.mTeamId = teamId; + return res; + } + + /** + * Instantiate a new PersonTeamBean with all the values copied from the given model. + */ + @NonNull + public static PersonTeamBean copy(@NonNull PersonTeamModel from) { + PersonTeamBean res = new PersonTeamBean(); + res.mId = from.getId(); + res.mPersonId = from.getPersonId(); + res.mTeamId = from.getTeamId(); + return res; + } + + public static class Builder { + private PersonTeamBean mRes = new PersonTeamBean(); + + /** + * Primary key. + */ + public Builder id(long id) { + mRes.mId = id; + return this; + } + + /** + * Set the {@code person_id} value. + */ + public Builder personId(long personId) { + mRes.mPersonId = personId; + return this; + } + + /** + * Set the {@code team_id} value. + */ + public Builder teamId(long teamId) { + mRes.mTeamId = teamId; + return this; + } + + /** + * Get a new PersonTeamBean built with the given values. + */ + public PersonTeamBean build() { + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductBean.java b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductBean.java new file mode 100644 index 0000000..bb98f93 --- /dev/null +++ b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/product/ProductBean.java @@ -0,0 +1,176 @@ +/* + * This source is part of the + * _____ ___ ____ + * __ / / _ \/ _ | / __/___ _______ _ + * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ + * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / + * /___/ + * repository. + * + * Copyright (C) 2012-2015 Benoit 'BoD' Lubek (BoD@JRAF.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jraf.androidcontentprovidergenerator.sample.provider.product; + +// @formatter:off +import org.jraf.androidcontentprovidergenerator.sample.provider.base.BaseModel; + +import java.util.Date; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * A product that the company sells. + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class ProductBean implements ProductModel { + private long mProductId; + private String mName; + private Long mManualId; + + /** + * Get the {@code product_id} value. + */ + @Override + public long getProductId() { + return mProductId; + } + + /** + * Set the {@code product_id} value. + */ + public void setProductId(long productId) { + mProductId = productId; + } + + /** + * Get the {@code name} value. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getName() { + return mName; + } + + /** + * Set the {@code name} value. + * Must not be {@code null}. + */ + public void setName(@NonNull String name) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + mName = name; + } + + /** + * Optional manual id. + * Can be {@code null}. + */ + @Nullable + @Override + public Long getManualId() { + return mManualId; + } + + /** + * Optional manual id. + * Can be {@code null}. + */ + public void setManualId(@Nullable Long manualId) { + mManualId = manualId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ProductBean bean = (ProductBean) o; + return mProductId == bean.mProductId; + } + + @Override + public int hashCode() { + return (int) (mProductId ^ (mProductId >>> 32)); + } + + /** + * Instantiate a new ProductBean with specified values. + */ + @NonNull + public static ProductBean newInstance(long productId, @NonNull String name, @Nullable Long manualId) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + ProductBean res = new ProductBean(); + res.mProductId = productId; + res.mName = name; + res.mManualId = manualId; + return res; + } + + /** + * Instantiate a new ProductBean with all the values copied from the given model. + */ + @NonNull + public static ProductBean copy(@NonNull ProductModel from) { + ProductBean res = new ProductBean(); + res.mProductId = from.getProductId(); + res.mName = from.getName(); + res.mManualId = from.getManualId(); + return res; + } + + public static class Builder { + private ProductBean mRes = new ProductBean(); + + /** + * Set the {@code product_id} value. + */ + public Builder productId(long productId) { + mRes.mProductId = productId; + return this; + } + + /** + * Set the {@code name} value. + * Must not be {@code null}. + */ + public Builder name(@NonNull String name) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + mRes.mName = name; + return this; + } + + /** + * Optional manual id. + * Can be {@code null}. + */ + public Builder manualId(@Nullable Long manualId) { + mRes.mManualId = manualId; + return this; + } + + /** + * Get a new ProductBean built with the given values. + */ + public ProductBean build() { + if (mRes.mName == null) throw new IllegalArgumentException("name must not be null"); + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberBean.java b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberBean.java new file mode 100644 index 0000000..e2bd3c5 --- /dev/null +++ b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/serialnumber/SerialNumberBean.java @@ -0,0 +1,180 @@ +/* + * This source is part of the + * _____ ___ ____ + * __ / / _ \/ _ | / __/___ _______ _ + * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ + * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / + * /___/ + * repository. + * + * Copyright (C) 2012-2015 Benoit 'BoD' Lubek (BoD@JRAF.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jraf.androidcontentprovidergenerator.sample.provider.serialnumber; + +// @formatter:off +import org.jraf.androidcontentprovidergenerator.sample.provider.base.BaseModel; + +import java.util.Date; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * A serial number. + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class SerialNumberBean implements SerialNumberModel { + private long mId; + private String mPart0; + private String mPart1; + + /** + * Primary key. + */ + @Override + public long getId() { + return mId; + } + + /** + * Primary key. + */ + public void setId(long id) { + mId = id; + } + + /** + * Unique id, first part. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getPart0() { + return mPart0; + } + + /** + * Unique id, first part. + * Must not be {@code null}. + */ + public void setPart0(@NonNull String part0) { + if (part0 == null) throw new IllegalArgumentException("part0 must not be null"); + mPart0 = part0; + } + + /** + * Unique id, second part. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getPart1() { + return mPart1; + } + + /** + * Unique id, second part. + * Must not be {@code null}. + */ + public void setPart1(@NonNull String part1) { + if (part1 == null) throw new IllegalArgumentException("part1 must not be null"); + mPart1 = part1; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SerialNumberBean bean = (SerialNumberBean) o; + return mId == bean.mId; + } + + @Override + public int hashCode() { + return (int) (mId ^ (mId >>> 32)); + } + + /** + * Instantiate a new SerialNumberBean with specified values. + */ + @NonNull + public static SerialNumberBean newInstance(long id, @NonNull String part0, @NonNull String part1) { + if (part0 == null) throw new IllegalArgumentException("part0 must not be null"); + if (part1 == null) throw new IllegalArgumentException("part1 must not be null"); + SerialNumberBean res = new SerialNumberBean(); + res.mId = id; + res.mPart0 = part0; + res.mPart1 = part1; + return res; + } + + /** + * Instantiate a new SerialNumberBean with all the values copied from the given model. + */ + @NonNull + public static SerialNumberBean copy(@NonNull SerialNumberModel from) { + SerialNumberBean res = new SerialNumberBean(); + res.mId = from.getId(); + res.mPart0 = from.getPart0(); + res.mPart1 = from.getPart1(); + return res; + } + + public static class Builder { + private SerialNumberBean mRes = new SerialNumberBean(); + + /** + * Primary key. + */ + public Builder id(long id) { + mRes.mId = id; + return this; + } + + /** + * Unique id, first part. + * Must not be {@code null}. + */ + public Builder part0(@NonNull String part0) { + if (part0 == null) throw new IllegalArgumentException("part0 must not be null"); + mRes.mPart0 = part0; + return this; + } + + /** + * Unique id, second part. + * Must not be {@code null}. + */ + public Builder part1(@NonNull String part1) { + if (part1 == null) throw new IllegalArgumentException("part1 must not be null"); + mRes.mPart1 = part1; + return this; + } + + /** + * Get a new SerialNumberBean built with the given values. + */ + public SerialNumberBean build() { + if (mRes.mPart0 == null) throw new IllegalArgumentException("part0 must not be null"); + if (mRes.mPart1 == null) throw new IllegalArgumentException("part1 must not be null"); + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamBean.java b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamBean.java new file mode 100644 index 0000000..b847483 --- /dev/null +++ b/etc/sample/app/app/src/main/java/org/jraf/androidcontentprovidergenerator/sample/provider/team/TeamBean.java @@ -0,0 +1,232 @@ +/* + * This source is part of the + * _____ ___ ____ + * __ / / _ \/ _ | / __/___ _______ _ + * / // / , _/ __ |/ _/_/ _ \/ __/ _ `/ + * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / + * /___/ + * repository. + * + * Copyright (C) 2012-2015 Benoit 'BoD' Lubek (BoD@JRAF.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jraf.androidcontentprovidergenerator.sample.provider.team; + +// @formatter:off +import org.jraf.androidcontentprovidergenerator.sample.provider.base.BaseModel; + +import java.util.Date; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +/** + * A group of people who work together. + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class TeamBean implements TeamModel { + private long mId; + private long mCompanyId; + private String mName; + private String mCountryCode; + private long mSerialNumberId; + + /** + * Primary key. + */ + @Override + public long getId() { + return mId; + } + + /** + * Primary key. + */ + public void setId(long id) { + mId = id; + } + + /** + * Get the {@code company_id} value. + */ + @Override + public long getCompanyId() { + return mCompanyId; + } + + /** + * Set the {@code company_id} value. + */ + public void setCompanyId(long companyId) { + mCompanyId = companyId; + } + + /** + * Get the {@code name} value. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getName() { + return mName; + } + + /** + * Set the {@code name} value. + * Must not be {@code null}. + */ + public void setName(@NonNull String name) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + mName = name; + } + + /** + * 2 letter country code where this team operates. + * Cannot be {@code null}. + */ + @NonNull + @Override + public String getCountryCode() { + return mCountryCode; + } + + /** + * 2 letter country code where this team operates. + * Must not be {@code null}. + */ + public void setCountryCode(@NonNull String countryCode) { + if (countryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + mCountryCode = countryCode; + } + + /** + * The serial number of this team. + */ + @Override + public long getSerialNumberId() { + return mSerialNumberId; + } + + /** + * The serial number of this team. + */ + public void setSerialNumberId(long serialNumberId) { + mSerialNumberId = serialNumberId; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TeamBean bean = (TeamBean) o; + return mId == bean.mId; + } + + @Override + public int hashCode() { + return (int) (mId ^ (mId >>> 32)); + } + + /** + * Instantiate a new TeamBean with specified values. + */ + @NonNull + public static TeamBean newInstance(long id, long companyId, @NonNull String name, @NonNull String countryCode, long serialNumberId) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + if (countryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + TeamBean res = new TeamBean(); + res.mId = id; + res.mCompanyId = companyId; + res.mName = name; + res.mCountryCode = countryCode; + res.mSerialNumberId = serialNumberId; + return res; + } + + /** + * Instantiate a new TeamBean with all the values copied from the given model. + */ + @NonNull + public static TeamBean copy(@NonNull TeamModel from) { + TeamBean res = new TeamBean(); + res.mId = from.getId(); + res.mCompanyId = from.getCompanyId(); + res.mName = from.getName(); + res.mCountryCode = from.getCountryCode(); + res.mSerialNumberId = from.getSerialNumberId(); + return res; + } + + public static class Builder { + private TeamBean mRes = new TeamBean(); + + /** + * Primary key. + */ + public Builder id(long id) { + mRes.mId = id; + return this; + } + + /** + * Set the {@code company_id} value. + */ + public Builder companyId(long companyId) { + mRes.mCompanyId = companyId; + return this; + } + + /** + * Set the {@code name} value. + * Must not be {@code null}. + */ + public Builder name(@NonNull String name) { + if (name == null) throw new IllegalArgumentException("name must not be null"); + mRes.mName = name; + return this; + } + + /** + * 2 letter country code where this team operates. + * Must not be {@code null}. + */ + public Builder countryCode(@NonNull String countryCode) { + if (countryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + mRes.mCountryCode = countryCode; + return this; + } + + /** + * The serial number of this team. + */ + public Builder serialNumberId(long serialNumberId) { + mRes.mSerialNumberId = serialNumberId; + return this; + } + + /** + * Get a new TeamBean built with the given values. + */ + public TeamBean build() { + if (mRes.mName == null) throw new IllegalArgumentException("name must not be null"); + if (mRes.mCountryCode == null) throw new IllegalArgumentException("countryCode must not be null"); + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/etc/sample/app/build.gradle b/etc/sample/app/build.gradle index 218ba3f..b826212 100644 --- a/etc/sample/app/build.gradle +++ b/etc/sample/app/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.2' + classpath 'com.android.tools.build:gradle:2.3.0-alpha1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/pom.xml b/pom.xml index ac112e6..2fac6c3 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jraf android_contentprovider_generator - 1.10.0 + 1.11.0 jar GenerateAndroidProvider diff --git a/src/main/java/org/jraf/androidcontentprovidergenerator/Main.java b/src/main/java/org/jraf/androidcontentprovidergenerator/Main.java index 5988e22..d22edab 100644 --- a/src/main/java/org/jraf/androidcontentprovidergenerator/Main.java +++ b/src/main/java/org/jraf/androidcontentprovidergenerator/Main.java @@ -77,6 +77,7 @@ public static class Json { public static final String ENABLE_FOREIGN_KEY = "enableForeignKeys"; public static final String USE_ANNOTATIONS = "useAnnotations"; public static final String USE_SUPPORT_LIBRARY = "useSupportLibrary"; + public static final String GENERATE_BEANS = "generateBeans"; } private Configuration mFreemarkerConfig; @@ -309,6 +310,7 @@ private void validateConfig() { ensureBoolean(Json.ENABLE_FOREIGN_KEY); ensureBoolean(Json.USE_ANNOTATIONS); ensureBoolean(Json.USE_SUPPORT_LIBRARY, false); + ensureBoolean(Json.GENERATE_BEANS, true); } private void ensureString(String field) { @@ -394,6 +396,31 @@ private void generateModels(Arguments arguments) throws IOException, JSONExcepti } } + private void generateBeans(Arguments arguments) throws IOException, JSONException, TemplateException { + Template template = getFreeMarkerConfig().getTemplate("bean.ftl"); + JSONObject config = getConfig(arguments.inputDir); + String providerJavaPackage = config.getString(Json.PROVIDER_JAVA_PACKAGE); + + File providerDir = new File(arguments.outputDir, providerJavaPackage.replace('.', '/')); + Map root = new HashMap<>(); + root.put("config", getConfig(arguments.inputDir)); + root.put("header", Model.get().getHeader()); + root.put("model", Model.get()); + + // Entities + for (Entity entity : Model.get().getEntities()) { + File outputDir = new File(providerDir, entity.getPackageName()); + outputDir.mkdirs(); + File outputFile = new File(outputDir, entity.getNameCamelCase() + "Bean.java"); + Writer out = new OutputStreamWriter(new FileOutputStream(outputFile)); + + root.put("entity", entity); + + template.process(root, out); + IOUtils.closeQuietly(out); + } + } + private void generateWrappers(Arguments arguments) throws IOException, JSONException, TemplateException { JSONObject config = getConfig(arguments.inputDir); String providerJavaPackage = config.getString(Json.PROVIDER_JAVA_PACKAGE); @@ -573,6 +600,9 @@ private void go(String[] args) throws IOException, JSONException, TemplateExcept generateColumns(arguments); generateWrappers(arguments); generateModels(arguments); + if (mConfig.getBoolean(Json.GENERATE_BEANS)) { + generateBeans(arguments); + } generateContentProvider(arguments); generateSqliteOpenHelper(arguments); generateSqliteOpenHelperCallbacks(arguments); diff --git a/src/main/java/org/jraf/androidcontentprovidergenerator/model/Entity.java b/src/main/java/org/jraf/androidcontentprovidergenerator/model/Entity.java index 4a6bfb2..7c74c4b 100644 --- a/src/main/java/org/jraf/androidcontentprovidergenerator/model/Entity.java +++ b/src/main/java/org/jraf/androidcontentprovidergenerator/model/Entity.java @@ -140,6 +140,13 @@ public Field getFieldByName(String fieldName) { return null; } + public Field getIdField() { + for (Field field : getFields()) { + if (field.getIsId()) return field; + } + return null; + } + public void addConstraint(Constraint constraint) { mConstraints.add(constraint); } diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/bean.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/bean.ftl new file mode 100644 index 0000000..f744a26 --- /dev/null +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/bean.ftl @@ -0,0 +1,169 @@ +<#if header??> +${header} + +package ${config.providerJavaPackage}.${entity.packageName}; + +// @formatter:off +import ${config.providerJavaPackage}.base.BaseModel; + +import java.util.Date; +<#if config.useAnnotations> + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + + +/** +<#if entity.documentation??> + * ${entity.documentation} +<#else> + * Bean for the {@code ${entity.nameLowerCase}} table. + + */ +@SuppressWarnings({"WeakerAccess", "unused", "ConstantConditions"}) +public class ${entity.nameCamelCase}Bean implements ${entity.nameCamelCase}Model { + <#list entity.getFields() as field> + private ${field.javaTypeSimpleName} m${field.nameCamelCase}; + + <#list entity.getFields() as field> + + /** + <#if field.documentation??> + * ${field.documentation} + <#else> + * Get the {@code ${field.nameLowerCase}} value. + + <#if field.isNullable> + * Can be {@code null}. + <#else> + <#if !field.type.hasNotNullableJavaType()> + * Cannot be {@code null}. + + + */ + <#if config.useAnnotations> + <#if field.isNullable> + @Nullable + <#else> + <#if !field.type.hasNotNullableJavaType()> + @NonNull + + + + @Override + public ${field.javaTypeSimpleName} get${field.nameCamelCase}() { + return m${field.nameCamelCase}; + } + + /** + <#if field.documentation??> + * ${field.documentation} + <#else> + * Set the {@code ${field.nameLowerCase}} value. + + <#if field.isNullable> + * Can be {@code null}. + <#else> + <#if !field.type.hasNotNullableJavaType()> + * Must not be {@code null}. + + + */ + public void set${field.nameCamelCase}(<#if config.useAnnotations><#if field.isNullable>@Nullable <#else><#if !field.type.hasNotNullableJavaType()>@NonNull ${field.javaTypeSimpleName} ${field.nameCamelCaseLowerCase}) { + <#if !field.isNullable && !field.type.hasNotNullableJavaType()> + if (${field.nameCamelCaseLowerCase} == null) throw new IllegalArgumentException("${field.nameCamelCaseLowerCase} must not be null"); + + m${field.nameCamelCase} = ${field.nameCamelCaseLowerCase}; + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ${entity.nameCamelCase}Bean bean = (${entity.nameCamelCase}Bean) o; + return m${entity.idField.nameCamelCase} == bean.m${entity.idField.nameCamelCase}; + } + + @Override + public int hashCode() { + return (int) (m${entity.idField.nameCamelCase} ^ (m${entity.idField.nameCamelCase} >>> 32)); + } + + /** + * Instantiate a new ${entity.nameCamelCase}Bean with specified values. + */ + <#if config.useAnnotations> + @NonNull + + public static ${entity.nameCamelCase}Bean newInstance(<#list entity.getFields() as field><#if config.useAnnotations><#if field.isNullable>@Nullable <#else><#if !field.type.hasNotNullableJavaType()>@NonNull ${field.javaTypeSimpleName} ${field.nameCamelCaseLowerCase}<#if field_has_next>, ) { + <#list entity.getFields() as field> + <#if !field.isNullable && !field.type.hasNotNullableJavaType()> + if (${field.nameCamelCaseLowerCase} == null) throw new IllegalArgumentException("${field.nameCamelCaseLowerCase} must not be null"); + + + ${entity.nameCamelCase}Bean res = new ${entity.nameCamelCase}Bean(); + <#list entity.getFields() as field> + res.m${field.nameCamelCase} = ${field.nameCamelCaseLowerCase}; + + return res; + } + + /** + * Instantiate a new ${entity.nameCamelCase}Bean with all the values copied from the given model. + */ + <#if config.useAnnotations> + @NonNull + + public static ${entity.nameCamelCase}Bean copy(<#if config.useAnnotations>@NonNull ${entity.nameCamelCase}Model from) { + ${entity.nameCamelCase}Bean res = new ${entity.nameCamelCase}Bean(); + <#list entity.getFields() as field> + res.m${field.nameCamelCase} = from.get${field.nameCamelCase}(); + + return res; + } + + public static class Builder { + private ${entity.nameCamelCase}Bean mRes = new ${entity.nameCamelCase}Bean(); + + <#list entity.getFields() as field> + /** + <#if field.documentation??> + * ${field.documentation} + <#else> + * Set the {@code ${field.nameLowerCase}} value. + + <#if field.isNullable> + * Can be {@code null}. + <#else> + <#if !field.type.hasNotNullableJavaType()> + * Must not be {@code null}. + + + */ + public Builder ${field.nameCamelCaseLowerCase}(<#if config.useAnnotations><#if field.isNullable>@Nullable <#else><#if !field.type.hasNotNullableJavaType()>@NonNull ${field.javaTypeSimpleName} ${field.nameCamelCaseLowerCase}) { + <#if !field.isNullable && !field.type.hasNotNullableJavaType()> + if (${field.nameCamelCaseLowerCase} == null) throw new IllegalArgumentException("${field.nameCamelCaseLowerCase} must not be null"); + + mRes.m${field.nameCamelCase} = ${field.nameCamelCaseLowerCase}; + return this; + } + + + /** + * Get a new ${entity.nameCamelCase}Bean built with the given values. + */ + public ${entity.nameCamelCase}Bean build() { + <#list entity.getFields() as field> + <#if !field.isNullable && !field.type.hasNotNullableJavaType()> + if (mRes.m${field.nameCamelCase} == null) throw new IllegalArgumentException("${field.nameCamelCaseLowerCase} must not be null"); + + + return mRes; + } + } + + public static Builder newBuilder() { + return new Builder(); + } +} diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/model.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/model.ftl index d125a06..1c9be48 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/model.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/model.ftl @@ -30,15 +30,15 @@ public interface ${entity.nameCamelCase}Model extends BaseModel { <#else> * Get the {@code ${field.nameLowerCase}} value. - <#if field.isNullable> + <#if field.isNullable> * Can be {@code null}. - <#else> - <#if !field.type.hasNotNullableJavaType()> + <#else> + <#if !field.type.hasNotNullableJavaType()> * Cannot be {@code null}. - + */ - <#if config.useAnnotations> + <#if config.useAnnotations> <#if field.isNullable> @Nullable <#else> @@ -46,7 +46,7 @@ public interface ${entity.nameCamelCase}Model extends BaseModel { @NonNull - - ${field.javaTypeSimpleName} get<#if field.isForeign>${field.path}${field.nameCamelCase}(); + + ${field.javaTypeSimpleName} get${field.nameCamelCase}(); }