MoneyWiseDepositCategoryBuilder.java
/*
* MoneyWise: Finance Application
* Copyright 2012-2026. Tony Washer
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.github.tonywasher.joceanus.moneywise.data.builder;
import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataSet;
import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositCategory;
import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseDepositCategoryClass;
import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseDepositCategoryType;
import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
/**
* DepositCategory Builder.
*/
public class MoneyWiseDepositCategoryBuilder {
/**
* DataSet.
*/
private final MoneyWiseDataSet theDataSet;
/**
* The CategoryName.
*/
private String theName;
/**
* The Parent.
*/
private MoneyWiseDepositCategory theParent;
/**
* The CategoryType.
*/
private MoneyWiseDepositCategoryType theType;
/**
* Constructor.
*
* @param pDataSet the dataSet
*/
public MoneyWiseDepositCategoryBuilder(final MoneyWiseDataSet pDataSet) {
theDataSet = pDataSet;
theDataSet.getDepositCategories().ensureMap();
}
/**
* Set Name.
*
* @param pName the name of the category.
* @return the builder
*/
public MoneyWiseDepositCategoryBuilder name(final String pName) {
final int myIndex = pName.lastIndexOf(':');
if (myIndex == -1) {
theName = pName;
theParent = null;
} else {
theName = pName.substring(myIndex + 1);
theParent = lookupCategory(pName.substring(0, myIndex));
}
return this;
}
/**
* Set the categoryType.
*
* @param pType the type of the category.
* @return the builder
*/
public MoneyWiseDepositCategoryBuilder type(final MoneyWiseDepositCategoryType pType) {
theType = pType;
return this;
}
/**
* Set the categoryType.
*
* @param pType the type of the category.
* @return the builder
*/
public MoneyWiseDepositCategoryBuilder type(final MoneyWiseDepositCategoryClass pType) {
return type(theDataSet.getDepositCategoryTypes().findItemByClass(pType));
}
/**
* Obtain the depositCategory.
*
* @param pCategory the name of the category.
* @return the depositCategory
*/
private MoneyWiseDepositCategory lookupCategory(final String pCategory) {
return theDataSet.getDepositCategories().findItemByName(pCategory);
}
/**
* Build the depositCategory.
*
* @return the new Category
* @throws OceanusException on error
*/
public MoneyWiseDepositCategory build() throws OceanusException {
/* Create the category */
final MoneyWiseDepositCategory myCategory = theDataSet.getDepositCategories().addNewItem();
myCategory.setCategoryType(theType);
myCategory.setParentCategory(theParent);
myCategory.setSubCategoryName(theName);
/* Reset the values */
reset();
/* Check for errors */
myCategory.adjustMapForItem();
myCategory.validate();
if (myCategory.hasErrors()) {
myCategory.removeItem();
throw new MoneyWiseDataException(myCategory, "Failed validation");
}
/* Return the category */
return myCategory;
}
/**
* Reset the builder.
*/
public void reset() {
/* Reset values */
theName = null;
theParent = null;
theType = null;
}
}