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/DateConverter.java
40951 views
1
/*
2
* Copyright (c) 2015, 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-2015 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.DateFormat;
59
import java.text.ParsePosition;
60
import java.text.SimpleDateFormat;
61
import java.util.Date;
62
import java.util.Locale;
63
64
import jdk.internal.joptsimple.ValueConversionException;
65
import jdk.internal.joptsimple.ValueConverter;
66
import jdk.internal.joptsimple.internal.Messages;
67
68
/**
69
* Converts values to {@link Date}s using a {@link DateFormat} object.
70
*
71
* @author <a href="mailto:[email protected]">Paul Holser</a>
72
*/
73
public class DateConverter implements ValueConverter<Date> {
74
private final DateFormat formatter;
75
76
/**
77
* Creates a converter that uses the given date formatter/parser.
78
*
79
* @param formatter the formatter/parser to use
80
* @throws NullPointerException if {@code formatter} is {@code null}
81
*/
82
public DateConverter( DateFormat formatter ) {
83
if ( formatter == null )
84
throw new NullPointerException( "illegal null formatter" );
85
86
this.formatter = formatter;
87
}
88
89
/**
90
* Creates a converter that uses a {@link SimpleDateFormat} with the given date/time pattern. The date formatter
91
* created is not {@link SimpleDateFormat#setLenient(boolean) lenient}.
92
*
93
* @param pattern expected date/time pattern
94
* @return the new converter
95
* @throws NullPointerException if {@code pattern} is {@code null}
96
* @throws IllegalArgumentException if {@code pattern} is invalid
97
*/
98
public static DateConverter datePattern( String pattern ) {
99
SimpleDateFormat formatter = new SimpleDateFormat( pattern );
100
formatter.setLenient( false );
101
102
return new DateConverter( formatter );
103
}
104
105
public Date convert( String value ) {
106
ParsePosition position = new ParsePosition( 0 );
107
108
Date date = formatter.parse( value, position );
109
if ( position.getIndex() != value.length() )
110
throw new ValueConversionException( message( value ) );
111
112
return date;
113
}
114
115
public Class<Date> valueType() {
116
return Date.class;
117
}
118
119
public String valuePattern() {
120
return formatter instanceof SimpleDateFormat
121
? ( (SimpleDateFormat) formatter ).toPattern()
122
: "";
123
}
124
125
private String message( String value ) {
126
String key;
127
Object[] arguments;
128
129
if ( formatter instanceof SimpleDateFormat ) {
130
key = "with.pattern.message";
131
arguments = new Object[] { value, ( (SimpleDateFormat) formatter ).toPattern() };
132
} else {
133
key = "without.pattern.message";
134
arguments = new Object[] { value };
135
}
136
137
return Messages.message(
138
Locale.getDefault(),
139
"jdk.internal.joptsimple.ExceptionMessages",
140
DateConverter.class,
141
key,
142
arguments );
143
}
144
}
145
146