Source code: com/clra/web/MemberNameFormat.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: MemberNameFormat.java,v $
5 * $Date: 2003/02/26 03:38:46 $
6 * $Revision: 1.4 $
7 */
8
9 package com.clra.web;
10
11 import com.clra.member.MemberName;
12 import java.io.Serializable;
13
14 /**
15 * Formats the name of member according to a few different patterns.
16 * @version $Revision: 1.4 $ $Date: 2003/02/26 03:38:46 $
17 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
18 */
19 public abstract class MemberNameFormat implements Serializable {
20
21 protected MemberNameFormat() {}
22
23 /**
24 * Returns a non-null, non-blank, trimmed String representing the name
25 * of a member. This method is a template. Subclasses should implement
26 * <tt>protectedFormat(MemberName)</tt> to define new formatting behaviour.
27 * @param memberName a non-null MemberName (enforced)
28 * @return a non-null, non-blank String (enforced)
29 */
30 public final String format( MemberName memberName ) {
31 preConditions(memberName);
32 String retVal = protectedFormat(memberName).trim();
33 postConditions(retVal);
34 return retVal;
35 }
36
37 private final static void preConditions( MemberName memberName ) {
38 if ( memberName == null ) {
39 throw new IllegalArgumentException( "null member name" );
40 }
41 }
42
43 private final static void postConditions( String retVal ) {
44 if ( retVal == null || retVal.length() == 0 ) {
45 throw new IllegalStateException( "invalid return value" );
46 }
47 }
48
49 /**
50 * Subclasses must implement this method to define formatting behavior.
51 * Subclasses must return a non-null, non-blank String. They do not
52 * have to check that the member name is valid; that check has already
53 * been performed. Nor do they have to trim the return value; that task
54 * will be automatically performed before the return value is passed back.
55 */
56 protected abstract String protectedFormat( MemberName memberName );
57
58 /**
59 * Returns a string of the form:<pre>
60 * First
61 * </pre>
62 */
63 public final static MemberNameFormat FIRSTONLY =
64
65 new MemberNameFormat() {
66
67 protected String protectedFormat( MemberName memberName ) {
68 return memberName.getFirstName();
69 }
70
71 }; // FIRSTONLY
72
73 /**
74 * Returns a string of the form:<pre>
75 * First [Middle] Last [Suffix]
76 * </pre>
77 */
78 public final static MemberNameFormat FIRSTLAST =
79
80 new MemberNameFormat() {
81
82 protected String protectedFormat( MemberName memberName ) {
83 StringBuffer sb = new StringBuffer();
84 String test = memberName.getFirstName();
85 sb.append( test );
86 test = memberName.getMiddleName();
87 if ( test != null ) {
88 sb.append( " " + test );
89 }
90 test = memberName.getLastName();
91 sb.append( " " + test );
92 test = memberName.getSuffix();
93 if ( test != null ) {
94 sb.append( " " + test );
95 }
96 String retVal = new String( sb );
97
98 return retVal;
99 } // protectedFormat(MemberName)
100
101 }; // FIRSTLAST
102
103 /**
104 * Returns a string of the form:<pre>
105 * Last, First [Middle] [Suffix]
106 * </pre>
107 */
108 public final static MemberNameFormat LASTFIRST =
109
110 new MemberNameFormat() {
111
112 protected String protectedFormat( MemberName memberName ) {
113 StringBuffer sb = new StringBuffer();
114 String test = memberName.getLastName();
115 sb.append( test );
116 test = memberName.getFirstName();
117 sb.append( ", " + test );
118 test = memberName.getMiddleName();
119 if ( test != null ) {
120 sb.append( " " + test );
121 }
122 test = memberName.getSuffix();
123 if ( test != null ) {
124 sb.append( " " + test );
125 }
126 String retVal = new String( sb );
127
128 return retVal;
129 } // protectedFormat(MemberName)
130
131 }; // LASTFIRST
132
133 } // MemberNameFormat
134
135 /*
136 * $Log: MemberNameFormat.java,v $
137 * Revision 1.4 2003/02/26 03:38:46 rphall
138 * Added copyright and GPL license
139 *
140 * Revision 1.3 2003/02/19 22:38:39 rphall
141 * Removed gratuitous use of CLRA acronym
142 *
143 * Revision 1.2 2002/02/18 18:06:19 rphall
144 * Ran dos2unix to remove ^M (carriage return) from end of lines
145 */
146