001/* 002 * Shredzone Commons - pdb 003 * 004 * Copyright (C) 2009 Richard "Shred" Körber 005 * http://commons.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Library General Public License as 009 * published by the Free Software Foundation, either version 3 of the 010 * License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU Library General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020package org.shredzone.commons.pdb.appinfo; 021 022import java.util.ArrayList; 023import java.util.List; 024 025/** 026 * A standard {@link AppInfo} container that contains a list of category names. 027 */ 028public class CategoryAppInfo implements AppInfo { 029 030 private List<Category> categories = new ArrayList<>(); 031 032 /** 033 * Gets a list of category names. 034 */ 035 public List<Category> getCategories() { return categories; } 036 037 /** 038 * Finds a {@link Category} by its index. 039 * 040 * @param index 041 * Category index 042 * @return {@link Category} or {@code null} 043 */ 044 public Category getCategoryByIndex(int index) { 045 return categories.get(index); 046 } 047 048 /** 049 * Finds a {@link Category} by its key. 050 * 051 * @param key 052 * Category key 053 * @return {@link Category} or {@code null} 054 */ 055 public Category getCategoryByKey(int key) { 056 for (Category cat : categories) { 057 if (cat != null && cat.getKey() == key) { 058 return cat; 059 } 060 } 061 return null; 062 } 063 064 /** 065 * Finds a {@link Category} index by the category key. 066 * 067 * @param key 068 * Category key 069 * @return Index of that category, or -1 if there was none with that key. 070 */ 071 public int findCategoryByKey(int key) { 072 for (int ix = 0; ix < categories.size(); ix++) { 073 Category cat = categories.get(ix); 074 if (cat != null && cat.getKey() == key) { 075 return ix; 076 } 077 } 078 079 return -1; 080 } 081 082 /** 083 * Finds a {@link Category} index by the category name. 084 * 085 * @param name 086 * Category name 087 * @return Index of that category, or -1 if there was none with that name. 088 */ 089 public int findCategoryByName(String name) { 090 for (int ix = 0; ix < categories.size(); ix++) { 091 Category cat = categories.get(ix); 092 if (cat != null && cat.getName().equals(name)) { 093 return ix; 094 } 095 } 096 097 return -1; 098 } 099 100 /** 101 * A single category. 102 */ 103 public static class Category { 104 private final String name; 105 private final int key; 106 private final boolean renamed; 107 108 public Category(String name, int key, boolean renamed) { 109 this.name = name; 110 this.key = key; 111 this.renamed = renamed; 112 } 113 114 public String getName() { return name; } 115 public int getKey() { return key; } 116 public boolean isRenamed() { return renamed; } 117 118 @Override 119 public String toString() { 120 return name + " (" + key + ")"; 121 } 122 } 123 124}