KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > stringifier > InterfacesStringifier


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.stringifier;
24
25 /**
26     Stringifies an object based on specified interfaces.
27  */

28 public class InterfacesStringifier implements Stringifier
29 {
30     private final StringifierRegistry mRegistry;
31     private final Class JavaDoc<?>[] mInterfaces;
32     
33         public
34     InterfacesStringifier( Class JavaDoc[] interfaces )
35     {
36         this( StringifierRegistryImpl.DEFAULT, interfaces );
37     }
38     
39         public
40     InterfacesStringifier( StringifierRegistry registry, Class JavaDoc[] interfaces)
41     {
42         mRegistry = registry;
43         mInterfaces = interfaces;
44     }
45     
46         private <T> String JavaDoc
47     stringifyAs( Object JavaDoc o, Class JavaDoc<T> theClass )
48     {
49         String JavaDoc result = null;
50         if ( theClass.isAssignableFrom( o.getClass() ) )
51         {
52             final Stringifier stringifier = mRegistry.lookup( theClass );
53             if ( stringifier != null )
54             {
55                 result = stringifier.stringify( o );
56             }
57         }
58         return( result );
59     }
60     
61         public String JavaDoc
62     stringify( Object JavaDoc o )
63     {
64         String JavaDoc result = "";
65         
66         for( int i = 0; i < mInterfaces.length; ++i )
67         {
68             final Class JavaDoc<?> intf = mInterfaces[ i ];
69             
70             final String JavaDoc s = stringifyAs( o, intf );
71             if ( s != null )
72             {
73                 result = result + intf.getName() + ": " + s + "\n";
74             }
75         }
76         
77         if ( result == null || result.length() == 0)
78         {
79             result = o.toString();
80         }
81     
82         return( result );
83     }
84 }
85
86
87
88
89
90
91
92
93
Popular Tags