org.openlca.io.CategoryPair Maven / Gradle / Ivy
package org.openlca.io;
import org.openlca.core.database.EntityCache;
import org.openlca.core.model.Category;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.util.Strings;
/**
* A simple pair of a parent category and a sub-category. Often this is
* appropriate for the display of elementary flow categories (= compartment and
* sub-compartment.
*/
public class CategoryPair implements Comparable {
private String category;
private String subCategory;
public static CategoryPair create(FlowDescriptor flow, EntityCache cache) {
if (flow == null || flow.category == null)
return new CategoryPair(null);
Category category = cache.get(Category.class, flow.category);
return new CategoryPair(category);
}
public CategoryPair(String category, String subCategory) {
initVals(category, subCategory);
}
public CategoryPair(Category category) {
if (category == null)
initVals(null, null);
else if (category.category == null)
initVals(category.name, null);
else
initVals(category.category.name, category.name);
}
private void initVals(String category, String subCategory) {
this.category = category == null ? "" : category;
this.subCategory = subCategory == null ? "" : subCategory;
}
public String getCategory() {
return category;
}
public String getSubCategory() {
return subCategory;
}
@Override
public int compareTo(CategoryPair other) {
if (other == null)
return 1;
int c = Strings.compare(this.category, other.category);
if (c != 0)
return c;
return Strings.compare(this.subCategory, other.subCategory);
}
public String toPath() {
if (category == null || category.isEmpty())
return subCategory;
else
return category + "/" + subCategory;
}
}