1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package net.jini.core.lookup;
19
20 import java.io.IOException;
21 import org.apache.river.api.io.AtomicSerial;
22 import org.apache.river.api.io.AtomicSerial.GetArg;
23
24 /**
25 * An instance of this class is used for the return value when looking up
26 * multiple items in the lookup service.
27 *
28 * Public fields have been made final to ensure that ServiceItem's are safely
29 * published at the time ServiceMatches is constructed.
30 *
31 * @author Sun Microsystems, Inc.
32 *
33 * @since 1.0
34 */
35 @AtomicSerial
36 public class ServiceMatches implements java.io.Serializable {
37
38 private static final long serialVersionUID = -5518280843537399398L;
39
40 /**
41 * Matching items (up to maxMatches from lookup method).
42 *
43 * @serial
44 */
45 public final ServiceItem[] items;
46 /**
47 * Total number of matching items.
48 *
49 * @serial
50 */
51 public final int totalMatches;
52
53 /**
54 * {@link AtomicSerial} convenience constructor.
55 *
56 * Since this class is mutable it should be cloned during deserialization.
57 *
58 * @param arg atomic deserialization parameter
59 * @throws IOException if there are I/O errors while reading from GetArg's
60 * underlying <code>InputStream</code>
61 */
62 public ServiceMatches(GetArg arg) throws IOException {
63 // The only invariant is the ServiceItem[] type check, which is done
64 // before super() is called.
65 // arg can be null, required to pass ToStringTest legacy test.
66 this(arg == null? null: arg.get("items", null, ServiceItem[].class),
67 arg == null? 0: arg.get("totalMatches", 0));
68 }
69
70 /**
71 * Simple constructor.
72 *
73 * @param items matching items
74 * @param totalMatches total number of matching items
75 */
76 public ServiceMatches(ServiceItem[] items, int totalMatches) {
77 this.items = items;
78 this.totalMatches = totalMatches;
79 }
80
81 /**
82 * Returns a <code>String</code> representation of this
83 * <code>ServiceMatches</code>.
84 * @return <code>String</code> representation of this
85 * <code>ServiceMatches</code>
86 */
87 public String toString() {
88 StringBuffer sBuffer = new StringBuffer();
89 sBuffer.append(
90 getClass().getName()).append(
91 "[totalMatches=").append(
92 totalMatches).append(
93 ", items=");
94 if (items != null) {
95 sBuffer.append("[");
96 if (items.length > 0) {
97 for (int i = 0; items.length > 0 && i < items.length - 1; i++)
98 sBuffer.append(items[i]).append(" ");
99 sBuffer.append(items[items.length - 1]);
100 }
101 sBuffer.append("]");
102 } else {
103 sBuffer.append((Object)null);
104 }
105 return sBuffer.append("]").toString();
106 }
107 }