Source Code Cross Referenced for SCOHelper.java in  » Database-ORM » TJDO » com » triactive » jdo » sco » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » TJDO » com.triactive.jdo.sco 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 (C) TJDO.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the TJDO License version 1.0.
006:         * See the terms of the TJDO License in the documentation provided with this software.
007:         *
008:         * $Id: SCOHelper.java,v 1.1 2004/01/18 03:01:06 jackknifebarber Exp $
009:         */
010:
011:        package com.triactive.jdo.sco;
012:
013:        import com.triactive.jdo.SCO;
014:        import java.util.ArrayList;
015:        import java.util.Collection;
016:        import java.util.Iterator;
017:        import java.util.Map;
018:        import java.util.Map.Entry;
019:
020:        /**
021:         * Contains static methods to help mutable SCO classes.
022:         * Because SCO classes must inherit from specific base classes they cannot
023:         * share this code via inheritance.
024:         *
025:         * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
026:         * @version $Revision: 1.1 $
027:         */
028:
029:        class SCOHelper {
030:            private SCOHelper() {
031:            }
032:
033:            private static void assertIsValidElement(SCO o, boolean allowNulls,
034:                    Class expectedType, Object element) {
035:                if (element == null && !allowNulls)
036:                    throw new NullsNotAllowedException(o);
037:
038:                Class actualType = element.getClass();
039:
040:                if (!expectedType.isAssignableFrom(actualType))
041:                    throw new IncompatibleElementTypeException(o, expectedType,
042:                            actualType);
043:            }
044:
045:            private static void assertIsValidKey(SCO o, Class expectedType,
046:                    Object key) {
047:                if (key == null)
048:                    throw new NullsNotAllowedException(o);
049:
050:                Class actualType = key.getClass();
051:
052:                if (!expectedType.isAssignableFrom(actualType))
053:                    throw new IncompatibleKeyTypeException(o, expectedType,
054:                            actualType);
055:            }
056:
057:            private static void assertIsValidValue(SCO o, boolean allowNulls,
058:                    Class expectedType, Object value) {
059:                if (value == null && !allowNulls)
060:                    throw new NullsNotAllowedException(o);
061:
062:                Class actualType = value.getClass();
063:
064:                if (!expectedType.isAssignableFrom(actualType))
065:                    throw new IncompatibleValueTypeException(o, expectedType,
066:                            actualType);
067:            }
068:
069:            /**
070:             * Asserts that an element is valid for storage in the specified SCO
071:             * collection.
072:             *
073:             * @exception SCOException
074:             *      If the element is not valid.
075:             */
076:
077:            public static void assertIsValidElement(SCOCollection c,
078:                    Object element) {
079:                if (c.getOwner() != null)
080:                    assertIsValidElement(c, c.allowsNulls(),
081:                            c.getElementType(), element);
082:            }
083:
084:            /**
085:             * Asserts that a key is valid for storage in the specified SCO map.
086:             *
087:             * @exception SCOException
088:             *      If the key is not valid.
089:             */
090:
091:            public static void assertIsValidKey(SCOMap m, Object key) {
092:                if (m.getOwner() != null)
093:                    assertIsValidKey(m, m.getKeyType(), key);
094:            }
095:
096:            /**
097:             * Asserts that a value is valid for storage in the specified SCO map.
098:             *
099:             * @exception SCOException
100:             *      If the value is not valid.
101:             */
102:
103:            public static void assertIsValidValue(SCOMap m, Object value) {
104:                if (m.getOwner() != null)
105:                    assertIsValidValue(m, m.allowsNullValues(), m
106:                            .getValueType(), value);
107:            }
108:
109:            /**
110:             * Tests if an element is valid for storage in the specified SCO collection.
111:             *
112:             * @return  <code>true</code> if the element valid,
113:             *          <code>false</code> otherwise.
114:             */
115:
116:            public static boolean isValidElement(SCOCollection c, Object element) {
117:                if (c.getOwner() != null) {
118:                    try {
119:                        assertIsValidElement(c, c.allowsNulls(), c
120:                                .getElementType(), element);
121:                    } catch (Exception e) {
122:                        return false;
123:                    }
124:                }
125:
126:                return true;
127:            }
128:
129:            /**
130:             * Tests if a key is valid for storage in the specified SCO map.
131:             *
132:             * @return  <code>true</code> if the key valid,
133:             *          <code>false</code> otherwise.
134:             */
135:
136:            public static boolean isValidKey(SCOMap m, Object key) {
137:                if (m.getOwner() != null) {
138:                    try {
139:                        assertIsValidKey(m, m.getKeyType(), key);
140:                    } catch (Exception e) {
141:                        return false;
142:                    }
143:                }
144:
145:                return true;
146:            }
147:
148:            /**
149:             * Tests if a value is valid for storage in the specified SCO map.
150:             *
151:             * @return  <code>true</code> if the value valid,
152:             *          <code>false</code> otherwise.
153:             */
154:
155:            public static boolean isValidValue(SCOMap m, Object value) {
156:                if (m.getOwner() != null) {
157:                    try {
158:                        assertIsValidValue(m, m.allowsNullValues(), m
159:                                .getValueType(), value);
160:                    } catch (Exception e) {
161:                        return false;
162:                    }
163:                }
164:
165:                return true;
166:            }
167:
168:            /**
169:             * Asserts that a group of elements are all valid for storage in the
170:             * specified SCO collection.
171:             *
172:             * @exception IllegalArgumentsException
173:             *      If one or more of the elements are not valid.
174:             */
175:
176:            public static void assertAllValidElements(SCOCollection c,
177:                    Collection elements) {
178:                if (c.getOwner() != null) {
179:                    boolean allowNulls = c.allowsNulls();
180:                    Class expectedType = c.getElementType();
181:
182:                    ArrayList failures = new ArrayList();
183:                    Iterator i = elements.iterator();
184:
185:                    while (i.hasNext()) {
186:                        try {
187:                            assertIsValidElement(c, allowNulls, expectedType, i
188:                                    .next());
189:                        } catch (Throwable t) {
190:                            failures.add(t);
191:                        }
192:                    }
193:
194:                    if (!failures.isEmpty())
195:                        throw new IllegalArgumentsException(
196:                                c,
197:                                (Throwable[]) failures
198:                                        .toArray(new Throwable[failures.size()]));
199:                }
200:            }
201:
202:            /**
203:             * Asserts that all the entries of a map are valid for storage in the
204:             * specified SCO map.
205:             *
206:             * @exception IllegalArgumentsException
207:             *      If one or more of the entries are not valid.
208:             */
209:
210:            public static void assertAllValidEntries(SCOMap m, Map entries) {
211:                if (m.getOwner() != null) {
212:                    boolean allowNullValues = m.allowsNullValues();
213:                    Class keyType = m.getKeyType();
214:                    Class valueType = m.getValueType();
215:
216:                    ArrayList failures = new ArrayList();
217:                    Iterator i = entries.entrySet().iterator();
218:
219:                    while (i.hasNext()) {
220:                        try {
221:                            Entry e = (Entry) i.next();
222:                            assertIsValidKey(m, keyType, e.getKey());
223:                            assertIsValidValue(m, allowNullValues, valueType, e
224:                                    .getValue());
225:                        } catch (Throwable t) {
226:                            failures.add(t);
227:                        }
228:                    }
229:
230:                    if (!failures.isEmpty())
231:                        throw new IllegalArgumentsException(
232:                                m,
233:                                (Throwable[]) failures
234:                                        .toArray(new Throwable[failures.size()]));
235:                }
236:            }
237:
238:            /**
239:             * Returns a string representation of an SCO object suitable for use in an
240:             * exception or a log message.
241:             *
242:             * @return A string representation of the SCO for logging purposes.
243:             */
244:
245:            public static String toLogString(SCO sco) {
246:                StringBuffer sb = new StringBuffer("SCO ");
247:
248:                String scoClassName = sco.getClass().getName();
249:                sb.append(
250:                        scoClassName
251:                                .substring(scoClassName.lastIndexOf('.') + 1))
252:                        .append('@').append(System.identityHashCode(sco));
253:
254:                Object owner = sco.getOwner();
255:
256:                if (owner != null) {
257:                    String ownerClassName = owner.getClass().getName();
258:
259:                    sb.append(" for ").append(
260:                            ownerClassName.substring(ownerClassName
261:                                    .lastIndexOf('.') + 1)).append('@').append(
262:                            System.identityHashCode(owner)).append('.').append(
263:                            sco.getFieldName());
264:                }
265:
266:                return sb.toString();
267:            }
268:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.