View Javadoc
1   /*
2    * Created on Feb 16, 2009
3    *
4    * Copyright (c) 2009, The JUNG Authors 
5    *
6    * All rights reserved.
7    *
8    * This software is open-source under the BSD license; see either
9    * "license.txt" or
10   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
11   */
12  package edu.uci.ics.jung.visualization.decorators;
13  
14  import java.text.NumberFormat;
15  
16  import com.google.common.base.Function;
17  
18  /**
19   * Transforms inputs to String representations by chaining an input 
20   * {@code Number}-generating {@code Function} with an internal 
21   * {@code NumberFormat} instance.
22   * @author Joshua O'Madadhain
23   */
24  public class NumberFormattingTransformer<T> implements Function<T, String>
25  {
26      private Function<T, ? extends Number> values;
27      private NumberFormat formatter = NumberFormat.getInstance();
28      
29      public NumberFormattingTransformer(Function<T, ? extends Number> values)
30      {
31          this.values = values;
32      }
33  
34      /**
35       * Returns a formatted string for the input.
36       */
37      public String apply(T input)
38      {
39          return formatter.format(values.apply(input));
40      }
41  
42  }