View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.base;
33  
34  import com.google.common.escape.Escaper;
35  import com.google.common.html.HtmlEscapers;
36  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
37  import gnu.trove.map.hash.THashMap;
38  import java.io.IOException;
39  import java.io.Writer;
40  import java.util.Map;
41  import javax.annotation.concurrent.Immutable;
42  import org.spf4j.base.avro.Converters;
43  import org.spf4j.base.avro.Method;
44  
45  /**
46   * @author zoly
47   */
48  @Immutable
49  // using racy single check idiom makes findbugs think the Method obejct is mutable...
50  @SuppressFBWarnings("JCIP_FIELD_ISNT_FINAL_IN_IMMUTABLE_CLASS")
51  public final class Methods  {
52  
53    public static final Method ROOT = Converters.ROOT;
54  
55    private static final Map<String, Map<String, Method>> INSTANCE_REPO = new THashMap<>(1024);
56  
57  
58    private Methods() { }
59  
60    public static void writeHtml(final Method m, final Writer w) throws IOException {
61      Escaper htmlEscaper = HtmlEscapers.htmlEscaper();
62      w.append(htmlEscaper.escape(m.getName())).append(htmlEscaper.escape("@")).
63              append(htmlEscaper.escape(m.getDeclaringClass()));
64    }
65  
66    public static Method getMethod(final StackTraceElement elem) {
67      return getMethod(elem.getClassName(), elem.getMethodName());
68    }
69  
70    /*
71       * this function is to allow reuse of Method instances.
72       * not thread safe, use with care, see description for suppressed findbugs bug for more detail.
73     */
74    @SuppressFBWarnings("PMB_POSSIBLE_MEMORY")
75    public static synchronized Method getMethod(final String className, final String methodName) {
76      Map<String, Method> mtom = INSTANCE_REPO.get(className);
77      Method result;
78      if (mtom == null) {
79        mtom = new THashMap<>(5);
80        result = new Method(className, methodName);
81        mtom.put(methodName, result);
82        INSTANCE_REPO.put(className, mtom);
83      } else {
84        result = mtom.get(methodName);
85        if (result == null) {
86          result = new Method(className, methodName);
87          mtom.put(methodName, result);
88        }
89      }
90      return result;
91    }
92  
93    public static void writeTo(final Method m, final Appendable w) throws IOException {
94      w.append(m.getName()).append('@').append(m.getDeclaringClass());
95    }
96  
97    public static void writeTo(final Method m, final StringBuilder w) {
98      w.append(m.getName()).append('@').append(m.getDeclaringClass());
99    }
100 
101   public static CharSequence toCharSequence(final Method m) {
102     StringBuilder sb = new StringBuilder(32);
103     writeTo(m, sb);
104     return sb;
105   }
106 
107   public static String toString(final Method m) {
108     return toCharSequence(m).toString();
109   }
110 
111   public static Method from(final CharSequence cs) {
112     return from(cs, 0, cs.length());
113   }
114 
115   public static Method from(final CharSequence cs, final int start, final int end) {
116     int idx = CharSequences.indexOf(cs, start, end, '@');
117     if (idx < 0) {
118       throw new IllegalArgumentException("Invalid method representation: " + cs);
119     }
120     return getMethod(cs.subSequence(idx + 1, end).toString(), cs.subSequence(start, idx).toString());
121   }
122 
123 }