Spec-Zone .ru
спецификации, руководства, описания, API
|
001/* 002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. Oracle designates this 008 * particular file as subject to the "Classpath" exception as provided 009 * by Oracle in the LICENSE file that accompanied this code. 010 * 011 * This code is distributed in the hope that it will be useful, but WITHOUT 012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 014 * version 2 for more details (a copy is included in the LICENSE file that 015 * accompanied this code). 016 * 017 * You should have received a copy of the GNU General Public License version 018 * 2 along with this work; if not, write to the Free Software Foundation, 019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 020 * 021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 022 * or visit www.oracle.com if you need additional information or have any 023 * questions. 024 */ 025 026package javafx.util.converter; 027 028import java.text.DecimalFormat; 029import java.text.DecimalFormatSymbols; 030import java.text.NumberFormat; 031import java.text.ParseException; 032import java.util.Locale; 033import javafx.util.StringConverter; 034 035/** 036 * <p>{@link StringConverter} implementation for {@link Number} values.</p> 037 */ 038public class NumberStringConverter extends StringConverter<Number> { 039 040 // ------------------------------------------------------ Private properties 041 042 final Locale locale; 043 final String pattern; 044 final NumberFormat numberFormat; 045 046 // ------------------------------------------------------------ Constructors 047 public NumberStringConverter() { 048 this(Locale.getDefault()); 049 } 050 051 public NumberStringConverter(Locale locale) { 052 this(locale, null); 053 } 054 055 public NumberStringConverter(String pattern) { 056 this(Locale.getDefault(), pattern); 057 } 058 059 public NumberStringConverter(Locale locale, String pattern) { 060 this(locale, pattern, null); 061 } 062 063 public NumberStringConverter(NumberFormat numberFormat) { 064 this(null, null, numberFormat); 065 } 066 067 NumberStringConverter(Locale locale, String pattern, NumberFormat numberFormat) { 068 this.locale = locale; 069 this.pattern = pattern; 070 this.numberFormat = numberFormat; 071 } 072 073 // ------------------------------------------------------- Converter Methods 074 075 /** {@inheritDoc} */ 076 @Override public Number fromString(String value) { 077 try { 078 // If the specified value is null or zero-length, return null 079 if (value == null) { 080 return null; 081 } 082 083 value = value.trim(); 084 085 if (value.length() < 1) { 086 return null; 087 } 088 089 // Create and configure the parser to be used 090 NumberFormat parser = getNumberFormat(); 091 092 // Perform the requested parsing 093 return parser.parse(value); 094 } catch (ParseException ex) { 095 throw new RuntimeException(ex); 096 } 097 } 098 099 /** {@inheritDoc} */ 100 @Override public String toString(Number value) { 101 // If the specified value is null, return a zero-length String 102 if (value == null) { 103 return ""; 104 } 105 106 // Create and configure the formatter to be used 107 NumberFormat formatter = getNumberFormat(); 108 109 // Perform the requested formatting 110 return formatter.format(value); 111 } 112 113 /** 114 * <p>Return a <code>NumberFormat</code> instance to use for formatting 115 * and parsing in this {@link StringConverter}.</p> 116 */ 117 protected NumberFormat getNumberFormat() { 118 Locale _locale = locale == null ? Locale.getDefault() : locale; 119 120 if (numberFormat != null) { 121 return numberFormat; 122 } else if (pattern != null) { 123 DecimalFormatSymbols symbols = new DecimalFormatSymbols(_locale); 124 return new DecimalFormat(pattern, symbols); 125 } else { 126 return NumberFormat.getNumberInstance(_locale); 127 } 128 } 129}