001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library 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 GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.acacia.shared; 029 030import java.io.Serializable; 031import java.util.ArrayList; 032import java.util.Collections; 033import java.util.List; 034 035/** 036 * Serializable entity attribute implementation.<p> 037 */ 038public class CmsEntityAttribute implements Serializable { 039 040 /** Serial version id. */ 041 private static final long serialVersionUID = 8283921354261037725L; 042 043 /** The complex type values. */ 044 private List<CmsEntity> m_entityValues; 045 046 /** The attribute name. */ 047 private String m_name; 048 049 /** The simple type values. */ 050 private List<String> m_simpleValues; 051 052 /** 053 * Constructor. For serialization only.<p> 054 */ 055 protected CmsEntityAttribute() { 056 057 } 058 059 /** 060 * Creates a entity type attribute.<p> 061 * 062 * @param name the attribute name 063 * @param values the attribute values 064 * 065 * @return the newly created attribute 066 */ 067 public static CmsEntityAttribute createEntityAttribute(String name, List<CmsEntity> values) { 068 069 CmsEntityAttribute result = new CmsEntityAttribute(); 070 result.m_name = name; 071 result.m_entityValues = Collections.unmodifiableList(values); 072 return result; 073 } 074 075 /** 076 * Creates a simple type attribute.<p> 077 * 078 * @param name the attribute name 079 * @param values the attribute values 080 * 081 * @return the newly created attribute 082 */ 083 public static CmsEntityAttribute createSimpleAttribute(String name, List<String> values) { 084 085 CmsEntityAttribute result = new CmsEntityAttribute(); 086 result.m_name = name; 087 result.m_simpleValues = Collections.unmodifiableList(values); 088 return result; 089 } 090 091 /** 092 * Returns the attribute name.<p> 093 * 094 * @return the attribute name 095 */ 096 public String getAttributeName() { 097 098 return m_name; 099 } 100 101 /** 102 * Returns the first complex value in the list.<p> 103 * 104 * @return the first complex value 105 */ 106 public CmsEntity getComplexValue() { 107 108 return m_entityValues.get(0); 109 } 110 111 /** 112 * Returns the list of complex values.<p> 113 * 114 * @return the list of complex values 115 */ 116 public List<CmsEntity> getComplexValues() { 117 118 List<CmsEntity> result = new ArrayList<CmsEntity>(); 119 result.addAll(m_entityValues); 120 return Collections.unmodifiableList(result); 121 } 122 123 /** 124 * Returns the first simple value in the list.<p> 125 * 126 * @return the first simple value 127 */ 128 public String getSimpleValue() { 129 130 return m_simpleValues.get(0); 131 } 132 133 /** 134 * Returns the list of simple values.<p> 135 * 136 * @return the list of simple values 137 */ 138 public List<String> getSimpleValues() { 139 140 return Collections.unmodifiableList(m_simpleValues); 141 } 142 143 /** 144 * Returns the number of values set for this attribute.<p> 145 * 146 * @return the number of values 147 */ 148 public int getValueCount() { 149 150 if (isComplexValue()) { 151 return m_entityValues.size(); 152 } 153 return m_simpleValues.size(); 154 } 155 156 /** 157 * Returns if the is a complex type value.<p> 158 * 159 * @return <code>true</code> if this is a complex type value 160 */ 161 public boolean isComplexValue() { 162 163 return m_entityValues != null; 164 } 165 166 /** 167 * Returns if the is a simple type value.<p> 168 * 169 * @return <code>true</code> if this is a simple type value 170 */ 171 public boolean isSimpleValue() { 172 173 return m_simpleValues != null; 174 } 175 176 /** 177 * Returns if this is a single value attribute.<p> 178 * 179 * @return <code>true</code> if this is a single value attribute 180 */ 181 public boolean isSingleValue() { 182 183 if (isComplexValue()) { 184 return m_entityValues.size() == 1; 185 } 186 return m_simpleValues.size() == 1; 187 } 188}