1 package com.atlassian.vcache.internal;
2
3 import javax.annotation.Nullable;
4 import java.util.regex.Pattern;
5
6 /**
7 * Provides utility methods to validate a name as being legitimate for use with VCache.
8 *
9 * @since 1.0
10 */
11 public class NameValidator {
12 // If this simple implementation provides to be a performance bottleneck, then consider an optimized algorithm
13 // looking at the characters.
14 private static final Pattern LEGAL_PATTERN = Pattern.compile("^[\\w\\./\\-_\\$]+$");
15
16 /**
17 * Returns whether the specified name is valid for a product identifier.
18 *
19 * @param name the name to check
20 * @return whether the specified name is valid for a product identifier.
21 */
22 public static boolean isValidProductIdentifier(@Nullable String name) {
23 return (name != null) && LEGAL_PATTERN.matcher(name).matches();
24 }
25
26 /**
27 * Verifies that the specified name is valid for a product identifier,
28 * and throws an {@link IllegalArgumentException} if it not.
29 *
30 * @param name the name to verify
31 * @return the name if it is valid
32 */
33 public static String requireValidProductIdentifier(@Nullable String name) {
34 if (!isValidProductIdentifier(name)) {
35 throw new IllegalArgumentException("Invalid product identifier: " + name);
36 }
37
38 return name;
39 }
40
41 /**
42 * Returns whether the specified name is valid for a partition identifier.
43 *
44 * @param name the name to check
45 * @return whether the specified name is valid for a partition identifier.
46 */
47 public static boolean isValidPartitionIdentifier(@Nullable String name) {
48 return (name != null) && LEGAL_PATTERN.matcher(name).matches();
49 }
50
51 /**
52 * Verifies that the specified name is valid for a partition identifier,
53 * and throws an {@link IllegalArgumentException} if it not.
54 *
55 * @param name the name to verify
56 * @return the name if it is valid
57 */
58 public static String requireValidPartitionIdentifier(@Nullable String name) {
59 if (!isValidPartitionIdentifier(name)) {
60 throw new IllegalArgumentException("Invalid partition identifier: " + name);
61 }
62
63 return name;
64 }
65
66 /**
67 * Returns whether the specified name is valid for a key.
68 *
69 * @param name the name to check
70 * @return whether the specified name is valid for a key.
71 */
72 public static boolean isValidKeyName(@Nullable String name) {
73 return (name != null) && LEGAL_PATTERN.matcher(name).matches();
74 }
75
76 /**
77 * Verifies that the specified name is valid for a key, and throws an {@link IllegalArgumentException} if it
78 * not.
79 *
80 * @param name the name to verify
81 * @return the name if it is valid
82 */
83 public static String requireValidKeyName(@Nullable String name) {
84 if (!isValidKeyName(name)) {
85 throw new IllegalArgumentException("Invalid key name: " + name);
86 }
87
88 return name;
89 }
90
91 /**
92 * Returns whether the specified name is valid for a cache.
93 *
94 * @param name the name to check
95 * @return whether the specified name is valid for a cache.
96 */
97 public static boolean isValidCacheName(String name) {
98 return LEGAL_PATTERN.matcher(name).matches();
99 }
100
101 /**
102 * Verifies that the specified name is valid for a cache, and throws an {@link IllegalArgumentException} if it
103 * not.
104 *
105 * @param name the name to verify
106 * @return the name if it is valid
107 */
108 public static String requireValidCacheName(String name) {
109 if (!isValidCacheName(name)) {
110 throw new IllegalArgumentException("Invalid cache name: " + name);
111 }
112
113 return name;
114 }
115 }