1 /*
2 * Copyright (C) 2013 4th Line GmbH, Switzerland
3 *
4 * The contents of this file are subject to the terms of either the GNU
5 * Lesser General Public License Version 2 or later ("LGPL") or the
6 * Common Development and Distribution License Version 1 or later
7 * ("CDDL") (collectively, the "License"). You may not use this file
8 * except in compliance with the License. See LICENSE.txt for more
9 * information.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 */
15
16 package org.fourthline.cling.model.resource;
17
18 import org.fourthline.cling.model.ExpirationDetails;
19
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.util.List;
23
24 /**
25 * An addressable object, stored, managed, and accessible through the {@link org.fourthline.cling.registry.Registry}.
26 *
27 * @param <M> The type of the model object.
28 *
29 * @author Christian Bauer
30 */
31 public class Resource<M> {
32
33 private URI pathQuery;
34 private M model;
35
36 /**
37 * @param pathQuery The path and (optional) query URI parts of this resource.
38 * @param model The model object.
39 */
40 public Resource(URI pathQuery, M model) {
41 try {
42 this.pathQuery = new URI(null, null, pathQuery.getPath(), pathQuery.getQuery(), null);
43 } catch (URISyntaxException ex) {
44 throw new RuntimeException(ex);
45 }
46 this.model = model;
47 if (model == null) {
48 throw new IllegalArgumentException("Model instance must not be null");
49 }
50 }
51
52 public URI getPathQuery() {
53 return pathQuery;
54 }
55
56 public M getModel() {
57 return model;
58 }
59
60
61 /**
62 * @param pathQuery A relative URI.
63 * @return <code>true</code> if the given URI path and query matches the resource's path and query.
64 */
65 public boolean matches(URI pathQuery) {
66 return pathQuery.equals(getPathQuery());
67 }
68
69 /**
70 * Called periodically by the registry to maintain the resource.
71 * <p>
72 * NOOP by default.
73 * </p>
74 *
75 * @param pendingExecutions Add <code>Runnable</code>'s to this collection if maintenance code has to run in the background.
76 * @param expirationDetails The details of this resource's expiration, e.g. when it will expire.
77 */
78 public void maintain(List<Runnable> pendingExecutions,
79 ExpirationDetails expirationDetails) {
80 // Do nothing
81 }
82
83 /**
84 * Called by the registry when it stops, in the shutdown thread.
85 * <p>
86 * NOOP by default.
87 * </p>
88 */
89 public void shutdown() {
90 // Do nothing
91 }
92
93 @Override
94 public boolean equals(Object o) {
95 if (this == o) return true;
96 if (o == null || getClass() != o.getClass()) return false;
97
98 Resource resource = (Resource) o;
99
100 if (!getPathQuery().equals(resource.getPathQuery())) return false;
101
102 return true;
103 }
104
105 @Override
106 public int hashCode() {
107 return getPathQuery().hashCode();
108 }
109
110 @Override
111 public String toString() {
112 return "(" + getClass().getSimpleName() + ") URI: " + getPathQuery();
113 }
114
115 }