01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.examples.repeater;
18:
19: import org.apache.wicket.model.LoadableDetachableModel;
20:
21: /**
22: * detachable model for an instance of contact
23: *
24: * @author igor
25: *
26: */
27: public class DetachableContactModel extends LoadableDetachableModel {
28: private long id;
29:
30: protected ContactsDatabase getContactsDB() {
31: return DatabaseLocator.getDatabase();
32: }
33:
34: /**
35: * @param c
36: */
37: public DetachableContactModel(Contact c) {
38: this (c.getId());
39: }
40:
41: /**
42: * @param id
43: */
44: public DetachableContactModel(long id) {
45: if (id == 0) {
46: throw new IllegalArgumentException();
47: }
48: this .id = id;
49: }
50:
51: /**
52: * @see java.lang.Object#hashCode()
53: */
54: public int hashCode() {
55: return new Long(id).hashCode();
56: }
57:
58: /**
59: * used for dataview with ReuseIfModelsEqualStrategy item reuse strategy
60: *
61: * @see org.apache.wicket.markup.repeater.ReuseIfModelsEqualStrategy
62: * @see java.lang.Object#equals(java.lang.Object)
63: */
64: public boolean equals(final Object obj) {
65: if (obj == this ) {
66: return true;
67: } else if (obj == null) {
68: return false;
69: } else if (obj instanceof DetachableContactModel) {
70: DetachableContactModel other = (DetachableContactModel) obj;
71: return other.id == this .id;
72: }
73: return false;
74: }
75:
76: /**
77: * @see org.apache.wicket.model.LoadableDetachableModel#load()
78: */
79: protected Object load() {
80: // loads contact from the database
81: return getContactsDB().get(id);
82: }
83: }
|