KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > DataObjectUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DataObjectUtils.java,v 1.8 2007/01/07 06:14:01 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.opensubsystems.core.data.DataObject;
31
32 /**
33  * Utility methods for DataObject manipulation.
34  *
35  * @version $Id: DataObjectUtils.java,v 1.8 2007/01/07 06:14:01 bastafidli Exp $
36  * @author Miro Halas
37  * @code.reviewer Miro Halas
38  * @code.reviewed 1.5 2006/04/28 01:45:44 jlegeny
39  */

40 public final class DataObjectUtils
41 {
42    // Constants ////////////////////////////////////////////////////////////////
43

44    /**
45     * Constant for compare result flag - nothing has changed.
46     */

47    public static final byte COMPARE_ORDER_NO_CHANGE = 0;
48    
49    /**
50     * Constant for compare result flag - order changed.
51     */

52    public static final byte COMPARE_ORDER_CHANGED = 1;
53    
54    /**
55     * Constant for compare result flag - attributes changed.
56     */

57    public static final byte COMPARE_ATTRIBUTES_CHANGED = 2;
58    
59    /**
60     * Constant for compare result flag - lists changed (added or removed objects).
61     */

62    public static final byte COMPARE_CHANGED = 4;
63
64    // Constructor //////////////////////////////////////////////////////////////
65

66    /**
67     * Private constructor since this class cannot be instantiated
68     */

69    private DataObjectUtils(
70    )
71    {
72       // Do nothing
73
}
74
75    // Public methods ///////////////////////////////////////////////////////////
76

77    /**
78     * Parse IDs from collection of Data Objects to String.
79     *
80     * @param colDataObjects - collection of data objects to parse
81     * @param strDel - String deliminer
82     * @return String - parsed array
83     */

84    public static String JavaDoc parseCollectionIdsToString(
85       Collection JavaDoc colDataObjects,
86       String JavaDoc strDel
87    )
88    {
89       StringBuffer JavaDoc strbInts = new StringBuffer JavaDoc();
90       if ((colDataObjects != null) && (!colDataObjects.isEmpty()))
91       {
92          for (Iterator JavaDoc items = colDataObjects.iterator(); items.hasNext();)
93          {
94             if (strbInts.length() > 0)
95             {
96                strbInts.append(strDel);
97             }
98             strbInts.append(((DataObject)items.next()).getId());
99          }
100       }
101       return strbInts.toString();
102    }
103
104    /**
105     * Convert collection of data objects to map keyed by their Ids.
106     *
107     * @param colDataObjects - collection of data objects to parse
108     * @return Map - map of data objects keyed by ids
109     */

110    public static Map JavaDoc convertCollectionToMap(
111       Collection JavaDoc colDataObjects
112    )
113    {
114       Map JavaDoc mpData = Collections.EMPTY_MAP;
115       
116       if ((colDataObjects != null) && (!colDataObjects.isEmpty()))
117       {
118          DataObject data;
119          
120          mpData = new HashMap JavaDoc(colDataObjects.size());
121          for (Iterator JavaDoc items = colDataObjects.iterator(); items.hasNext();)
122          {
123             data = (DataObject)items.next();
124             mpData.put(data.getIdAsObject(), data);
125          }
126       }
127       return mpData;
128    }
129 }
130
Popular Tags