001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.isis.core.progmodel.facets.value.percentage; 021 022import java.text.DecimalFormat; 023import java.text.NumberFormat; 024import java.text.ParseException; 025 026import org.apache.isis.applib.adapters.EncoderDecoder; 027import org.apache.isis.applib.adapters.Parser; 028import org.apache.isis.applib.profiles.Localization; 029import org.apache.isis.applib.value.Percentage; 030import org.apache.isis.core.commons.config.ConfigurationConstants; 031import org.apache.isis.core.commons.config.IsisConfiguration; 032import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 033import org.apache.isis.core.metamodel.facetapi.Facet; 034import org.apache.isis.core.metamodel.facetapi.FacetHolder; 035import org.apache.isis.core.metamodel.facets.object.parseable.TextEntryParseException; 036import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderAndFacetAbstract; 037import org.apache.isis.core.progmodel.facets.object.value.ValueSemanticsProviderContext; 038import org.apache.isis.core.progmodel.facets.value.floats.FloatingPointValueFacet; 039 040public class PercentageValueSemanticsProvider extends ValueSemanticsProviderAndFacetAbstract<Percentage> implements FloatingPointValueFacet { 041 042 private static final NumberFormat PERCENTAGE_FORMAT = NumberFormat.getPercentInstance(); 043 private static final NumberFormat DECIMAL_FORMAT = NumberFormat.getNumberInstance(); 044 045 private static final Percentage DEFAULT_VALUE = new Percentage(0.0f); 046 private static final int TYPICAL_LENGTH = 12; 047 048 public static Class<? extends Facet> type() { 049 return FloatingPointValueFacet.class; 050 } 051 052 private NumberFormat format = PERCENTAGE_FORMAT; 053 054 /** 055 * Required because implementation of {@link Parser} and 056 * {@link EncoderDecoder}. 057 */ 058 public PercentageValueSemanticsProvider() { 059 this(null, null, null); 060 } 061 062 public PercentageValueSemanticsProvider(final FacetHolder holder, final IsisConfiguration configuration, final ValueSemanticsProviderContext context) { 063 super(type(), holder, Percentage.class, TYPICAL_LENGTH, Immutability.IMMUTABLE, EqualByContent.HONOURED, DEFAULT_VALUE, configuration, context); 064 065 final String formatRequired = configuration.getString(ConfigurationConstants.ROOT + "value.format.percentage"); 066 if (formatRequired == null) { 067 format = PERCENTAGE_FORMAT; 068 } else { 069 format = new DecimalFormat(formatRequired); 070 } 071 } 072 073 // ////////////////////////////////////////////////////////////////// 074 // Parser 075 // ////////////////////////////////////////////////////////////////// 076 077 @Override 078 protected Percentage doParse(final Object context, final String text) { 079 try { 080 return new Percentage(new Float(format.parse(text).floatValue())); 081 } catch (final ParseException e) { 082 try { 083 return new Percentage(asFloat(text)); 084 } catch (final ParseException ee) { 085 throw new TextEntryParseException("Not a number " + text, ee); 086 } 087 } 088 } 089 090 private Float asFloat(final String text) throws ParseException { 091 return new Float(DECIMAL_FORMAT.parse(text).floatValue()); 092 } 093 094 @Override 095 public String titleString(final Object value, final Localization localization) { 096 return titleString(format, value); 097 } 098 099 private String titleString(final NumberFormat formatter, final Object value) { 100 return value == null ? "" : format.format(((Percentage) value).floatValue()); 101 } 102 103 @Override 104 public String titleStringWithMask(final Object value, final String usingMask) { 105 return titleString(new DecimalFormat(usingMask), value); 106 } 107 108 // ////////////////////////////////////////////////////////////////// 109 // EncoderDecoder 110 // ////////////////////////////////////////////////////////////////// 111 112 @Override 113 protected String doEncode(final Object object) { 114 final Percentage per = (Percentage) object; 115 return String.valueOf(per.floatValue()); 116 } 117 118 @Override 119 protected Percentage doRestore(final String data) { 120 return new Percentage(Float.valueOf(data).floatValue()); 121 } 122 123 // ////////////////////////////////////////////////////////////////// 124 // FloatingPointValueFacet 125 // ////////////////////////////////////////////////////////////////// 126 127 @Override 128 public Float floatValue(final ObjectAdapter object) { 129 final Percentage per = (Percentage) object.getObject(); 130 return new Float(per.floatValue()); 131 } 132 133 @Override 134 public ObjectAdapter createValue(final Float value) { 135 return getAdapterManager().adapterFor(value); 136 } 137 138 // ////////////////////////////////////////////////////////////////// 139 // PropertyDefaultFacet 140 // ////////////////////////////////////////////////////////////////// 141 142 public Object getDefault(final ObjectAdapter inObject) { 143 return Float.valueOf(0.0f); 144 } 145 146 // //// toString //////////////////////////////////////////////////// 147 148 @Override 149 public String toString() { 150 return "PercentageValueSemanticsProvider: " + format; 151 } 152 153}