KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > inout > wsiftypes > InoutImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package inout.wsiftypes;
59
60 import inout.wsifservice.Inout;
61 import java.util.Date JavaDoc;
62 import java.util.Hashtable JavaDoc;
63 import java.util.Iterator JavaDoc;
64
65 import util.AddressUtility;
66
67 import addressbook.wsiftypes.Address;
68 import addressbook.wsiftypes.Phone;
69
70 /**
71  * Inout service used by InoutTest for various miscelleanous tests.
72  * @author Mark Whitlock
73  */

74 public class InoutImpl implements Inout {
75     private Hashtable JavaDoc name2AddressTable = new Hashtable JavaDoc();
76
77     public InoutImpl() {
78         addEntry(
79             "John B. Good",
80             new Address(
81                 123,
82                 "Main Street",
83                 "Anytown",
84                 "NY",
85                 12345,
86                 new Phone(123, "456", "7890")));
87         addEntry(
88             "Bob Q. Public",
89             new Address(
90                 456,
91                 "North Whatever",
92                 "Notown",
93                 "ME",
94                 12424,
95                 new Phone(987, "444", "5566")));
96     }
97
98     public void addEntry(String JavaDoc name, Address address) {
99         if (name != null && address != null)
100             name2AddressTable.put(name, address);
101     }
102
103     public void addEntry(String JavaDoc firstName, String JavaDoc lastName, Address address) {
104         if (firstName != null && lastName != null && address != null)
105             name2AddressTable.put(firstName + " " + lastName, address);
106     }
107
108     public Address getAddressFromName(String JavaDoc name)
109         throws IllegalArgumentException JavaDoc {
110         if (name == null)
111             return null;
112         return getAddressFromName(new Mutablestring(name));
113     }
114
115     public Address getAddressFromName(Mutablestring name)
116         throws IllegalArgumentException JavaDoc {
117         if (name == null)
118             return null;
119
120         String JavaDoc found = null;
121         int star = name.toString().indexOf("*");
122         if (star != -1) {
123             String JavaDoc trimmed = name.toString().substring(0, star);
124             Iterator JavaDoc it = name2AddressTable.keySet().iterator();
125             while (it.hasNext()) {
126                 String JavaDoc key = (String JavaDoc) it.next();
127                 if (key.startsWith(trimmed)) {
128                     found = key;
129                     break;
130                 }
131             }
132
133             if (found == null)
134                 throw new IllegalArgumentException JavaDoc(
135                     "Couldn't find " + name + " trimmed=" + trimmed);
136         } else
137             found = name.toString();
138
139         return (Address) name2AddressTable.get(found);
140     }
141
142     public boolean getAddressFromName(Mutablestring name, Address addr)
143         throws IllegalArgumentException JavaDoc {
144         Address newAddr = getAddressFromName(name);
145         if (newAddr == null)
146             return false;
147         AddressUtility addrUtil = new AddressUtility(newAddr);
148         addrUtil.copy(addr);
149         return true;
150     }
151
152     public int addNumbers(int[] nums) {
153         int result = 0;
154         for (int i = 0; i < nums.length; i++)
155             result += nums[i];
156         return result;
157     }
158
159     public Date JavaDoc getDate() {
160         return new Date JavaDoc();
161     }
162
163     public String JavaDoc whoami(String JavaDoc s) {
164         return new String JavaDoc("String");
165     }
166     public String JavaDoc whoami(float f) {
167         return new String JavaDoc("float");
168     }
169     public String JavaDoc whoami(int i) {
170         return new String JavaDoc("int");
171     }
172     public String JavaDoc whoami(Address a) {
173         return new String JavaDoc("Address");
174     }
175 }
176
Popular Tags