From 0769979b0314649f8ec8a3ba66bfd40ae8b4971a Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 30 May 2008 14:03:56 +0000 Subject: [PATCH] Added countEntrySet() so that you can do: for (CountHashMap.Entry entry : cmap.countEntrySet()) { which you can't do with the somewhat butchered current entrySet() implementation. Indeed, I'm not sure quite how one is supposed to use that. Perhaps in an iterator and cast the result to a CountHashMap.Entry? git-svn-id: https://samskivert.googlecode.com/svn/trunk@2319 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/CountHashMap.java | 16 ++++++--- src/java/com/samskivert/util/JDK14Logger.java | 33 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/java/com/samskivert/util/CountHashMap.java b/src/java/com/samskivert/util/CountHashMap.java index 8ae5f654..8a99765f 100644 --- a/src/java/com/samskivert/util/CountHashMap.java +++ b/src/java/com/samskivert/util/CountHashMap.java @@ -2,8 +2,8 @@ // $Id$ // // samskivert library - useful routines for java programs -// Copyright (C) 2001-2007 Michael Bayne -// +// Copyright (C) 2001-2008 Michael Bayne +// // This library is free software; you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as published // by the Free Software Foundation; either version 2.1 of the License, or @@ -134,6 +134,14 @@ public class CountHashMap extends HashMap return new CountEntrySet(super.entrySet()); } + /** + * Returns a set of {@link Entry} records which can be used to easily obtain our count. + */ + public Set> countEntrySet () + { + return new CountEntrySet(super.entrySet()); + } + protected static class CountEntryImpl implements Entry { @@ -156,7 +164,7 @@ public class CountHashMap extends HashMap { return _entry.getValue(); } - + public int hashCode () { return _entry.hashCode(); @@ -226,7 +234,7 @@ public class CountHashMap extends HashMap { return CountHashMap.this.size(); } - + public void clear () { CountHashMap.this.clear(); diff --git a/src/java/com/samskivert/util/JDK14Logger.java b/src/java/com/samskivert/util/JDK14Logger.java index 3cd4e1c7..17115b94 100644 --- a/src/java/com/samskivert/util/JDK14Logger.java +++ b/src/java/com/samskivert/util/JDK14Logger.java @@ -53,6 +53,39 @@ public class JDK14Logger implements Logger.Factory return getLogger(clazz.getName()); } + /** + * Infers the caller of a Logger method from the current stack trace. This can be used by + * wrappers to provide the correct calling class and method information to their underlying log + * implementation. + * + * @return a two element array containing { class name, method name } or { null, null } if the + * caller could not be inferred. + */ + protected String[] inferCaller () + { + String self = getClass().getName(); + + // locate ourselves in the call stack + StackTraceElement[] stack = (new Throwable()).getStackTrace(); + int ii = 0; + for (; ii < stack.length; ii++) { + if (self.equals(stack[ii].getClassName())) { + break; + } + } + System.err.println("Found self at " + ii); + // now locate the first thing that's not us, that's the caller + for (; ii < stack.length; ii++) { + String cname = stack[ii].getClassName(); + if (!cname.equals(self)) { + System.err.println("Found non-self at " + ii + " " + cname); + return new String[] { cname, stack[ii].getMethodName() }; + } + } + System.err.println("Failed to find non-self."); + return new String[] { null, null }; + } + protected static class Impl extends Logger { public Impl (java.util.logging.Logger impl)