1 /*
2 * Licensed to The Apereo Foundation under one or more contributor license
3 * agreements. See the NOTICE file distributed with this work for additional
4 * information regarding copyright ownership.
5 *
6 *
7 * The Apereo Foundation licenses this file to you under the Educational
8 * Community License, Version 2.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of the License
10 * at:
11 *
12 * http://opensource.org/licenses/ecl2.txt
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 *
20 */
21
22
23 package org.opencastproject.mediapackage.identifier;
24
25 import java.util.UUID;
26 import java.util.regex.Pattern;
27
28 import javax.xml.bind.annotation.XmlAccessType;
29 import javax.xml.bind.annotation.XmlAccessorType;
30 import javax.xml.bind.annotation.XmlType;
31 import javax.xml.bind.annotation.XmlValue;
32
33 /**
34 * Simple and straightforward implementation of the {@link Id} interface.
35 */
36 @XmlType
37 @XmlAccessorType(XmlAccessType.NONE)
38 public class IdImpl implements Id {
39
40 private static final Pattern pattern = Pattern.compile("[\\w-_.:;()]+");
41
42 /** The identifier */
43 @XmlValue
44 protected String id = null;
45
46 /**
47 * Needed for JAXB serialization
48 */
49 public IdImpl() {
50 }
51
52 /**
53 * Creates a new identifier.
54 *
55 * @param id
56 * the identifier
57 */
58 public IdImpl(final String id) {
59 if (!pattern.matcher(id).matches()) {
60 throw new IllegalArgumentException("Id must match " + pattern);
61 }
62 this.id = id;
63 }
64
65 @Override
66 public String toString() {
67 return id;
68 }
69
70 /**
71 * {@inheritDoc}
72 *
73 * @see java.lang.Object#equals(java.lang.Object)
74 */
75 @Override
76 public boolean equals(Object o) {
77 if (o instanceof IdImpl) {
78 IdImpl other = (IdImpl) o;
79 return id != null && other.id != null && id.equals(other.id);
80 }
81 return false;
82 }
83
84 /**
85 * {@inheritDoc}
86 *
87 * @see java.lang.Object#hashCode()
88 */
89 @Override
90 public int hashCode() {
91 return id.hashCode();
92 }
93
94 /**
95 * Generate a new UUID-based Id.
96 * @return New Id
97 */
98 public static Id fromUUID() {
99 return new IdImpl(UUID.randomUUID().toString());
100 }
101 }