Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/util/EnumConverter.java
40951 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
* This file is available under and governed by the GNU General Public
28
* License version 2 only, as published by the Free Software Foundation.
29
* However, the following notice accompanied the original version of this
30
* file:
31
*
32
* The MIT License
33
*
34
* Copyright (c) 2004-2014 Paul R. Holser, Jr.
35
*
36
* Permission is hereby granted, free of charge, to any person obtaining
37
* a copy of this software and associated documentation files (the
38
* "Software"), to deal in the Software without restriction, including
39
* without limitation the rights to use, copy, modify, merge, publish,
40
* distribute, sublicense, and/or sell copies of the Software, and to
41
* permit persons to whom the Software is furnished to do so, subject to
42
* the following conditions:
43
*
44
* The above copyright notice and this permission notice shall be
45
* included in all copies or substantial portions of the Software.
46
*
47
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
51
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
52
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
53
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54
*/
55
56
package jdk.internal.joptsimple.util;
57
58
import java.text.MessageFormat;
59
import java.util.EnumSet;
60
import java.util.Iterator;
61
import java.util.ResourceBundle;
62
63
import jdk.internal.joptsimple.ValueConversionException;
64
import jdk.internal.joptsimple.ValueConverter;
65
66
/**
67
* Converts values to {@link java.lang.Enum}s.
68
*
69
* @author <a href="mailto:[email protected]">Christian Ohr</a>
70
*/
71
public abstract class EnumConverter<E extends Enum<E>> implements ValueConverter<E> {
72
private final Class<E> clazz;
73
74
private String delimiters = "[,]";
75
76
/**
77
* This constructor must be called by subclasses, providing the enum class as the parameter.
78
*
79
* @param clazz enum class
80
*/
81
protected EnumConverter( Class<E> clazz ) {
82
this.clazz = clazz;
83
}
84
85
@Override
86
public E convert( String value ) {
87
for ( E each : valueType().getEnumConstants() ) {
88
if ( each.name().equalsIgnoreCase( value ) ) {
89
return each;
90
}
91
}
92
93
throw new ValueConversionException( message( value ) );
94
}
95
96
@Override
97
public Class<E> valueType() {
98
return clazz;
99
}
100
101
/**
102
* Sets the delimiters for the message string. Must be a 3-letter string,
103
* where the first character is the prefix, the second character is the
104
* delimiter between the values, and the 3rd character is the suffix.
105
*
106
* @param delimiters delimiters for message string. Default is [,]
107
*/
108
public void setDelimiters( String delimiters ) {
109
this.delimiters = delimiters;
110
}
111
112
@Override
113
public String valuePattern() {
114
EnumSet<E> values = EnumSet.allOf( valueType() );
115
116
StringBuilder builder = new StringBuilder();
117
builder.append( delimiters.charAt(0) );
118
for ( Iterator<E> i = values.iterator(); i.hasNext(); ) {
119
builder.append( i.next().toString() );
120
if ( i.hasNext() )
121
builder.append( delimiters.charAt( 1 ) );
122
}
123
builder.append( delimiters.charAt( 2 ) );
124
125
return builder.toString();
126
}
127
128
private String message( String value ) {
129
ResourceBundle bundle = ResourceBundle.getBundle( "jdk.internal.joptsimple.ExceptionMessages" );
130
Object[] arguments = new Object[] { value, valuePattern() };
131
String template = bundle.getString( EnumConverter.class.getName() + ".message" );
132
return new MessageFormat( template ).format( arguments );
133
}
134
}
135
136