public String toString() { StringBuilder buf = new StringBuilder(getClass().getSimpleName()); buf.append(" ["); StringUtil.fieldsToString(buf, this); buf.append("]"); return buf.toString(); }
/** Dumps a text representation of this percentiler to the supplied print stream. */ public void dump(PrintWriter out) { // obtain our maximum count int max = 0; for (int ii = 0; ii < BUCKET_COUNT; ii++) { if (_counts[ii] > max) { max = _counts[ii]; } } // figure out how many digits are needed to display the biggest bucket's size int digits = (int) Math.ceil(Math.log(max) / Math.log(10)); digits = Math.max(digits, 1); // output each bucket in a column of its own for (int rr = 9; rr >= 0; rr--) { // print the "value" of this row out.print(StringUtil.pad("" + (rr + 1) * max / 10, digits) + " "); for (int ii = 0; ii < BUCKET_COUNT; ii++) { out.print((_counts[ii] * 10 / max > rr) ? "*" : " "); } out.println(""); } out.print(spaces(digits)); for (int ii = 0; ii < BUCKET_COUNT; ii++) { out.print("-"); } out.println(""); out.print(spaces(digits)); for (int ii = 0; ii < BUCKET_COUNT; ii++) { out.print(_percentile[ii] % 10); } out.println(""); out.print(spaces(digits)); for (int ii = 0; ii < BUCKET_COUNT; ii++) { out.print((_percentile[ii] / 10) % 10); } out.println(""); // print out a scale along the very bottom out.println(""); out.println( "total: " + _total + " min: " + _min + " max: " + _max + " delta: " + ((float) _max / BUCKET_COUNT)); }
/** * Returns a list of all nodes registered in the repository with names starting with the given * string, optionally including nodes that are explicitly shut down. */ public List<NodeRecord> loadNodes(String namespace, boolean includeShutdown) { // we specifically avoid caching this query because we want the servers to always see the // most up to date set of nodes Query<NodeRecord> query = from(NodeRecord.class).noCache(); List<SQLExpression<?>> conditions = Lists.newArrayList(); if (!StringUtil.isBlank(namespace)) { conditions.add(NodeRecord.NODE_NAME.like(namespace + "%")); } if (!includeShutdown) { conditions.add(Ops.not(NodeRecord.SHUTDOWN)); } return (conditions.isEmpty() ? query : query.where(conditions)).select(); }
@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append("[total=").append(_total); buf.append(", min=").append(_min); buf.append(", max=").append(_max); buf.append(", pcts=("); for (int ii = 0; ii < 10; ii++) { if (ii > 0) { buf.append("-"); } buf.append(StringUtil.format(getRequiredScore(10 * ii))); } return buf.append(")]").toString(); }
@Override public void begin(String namespace, String name, Attributes attrs) throws Exception { Object top = digester.peek(); Class<?> topclass = top.getClass(); // iterate over the attributes, setting public fields where applicable for (int i = 0; i < attrs.getLength(); i++) { String lname = attrs.getLocalName(i); if (StringUtil.isBlank(lname)) { lname = attrs.getQName(i); } // look for a public field with this lname Field field = null; try { field = topclass.getField(lname); } catch (NoSuchFieldException nsfe) { if (_warnNonFields) { digester .getLogger() .warn("Skipping property '" + lname + "' for which there is no field."); } continue; } // convert the value into the appropriate object type String valstr = attrs.getValue(i); FieldParser parser = null; Object value; // look for a custom field parser if ((_parsers != null) && ((parser = _parsers.get(lname)) != null)) { value = parser.parse(valstr); } else { // otherwise use the value marshaller to parse the property based on the type of // the target object field value = ValueMarshaller.unmarshal(field.getType(), valstr); } if (digester.getLogger().isDebugEnabled()) { digester.getLogger().debug(" Setting property '" + lname + "' to '" + valstr + "'"); } // and finally set the field field.set(top, value); } }
/** * Returns an array containing the available subtypes of the specified type, first looking to * subtypes listed in the annotation, then attempting to find a method using reflection. */ protected Class<?>[] getSubtypes(Class<?> type) { ArrayList<Class<?>> types = new ArrayList<Class<?>>(); // start with the null class, if allowed boolean nullable = getAnnotation().nullable(); if (nullable) { types.add(null); } // look for a subtype annotation and add its types EditorTypes ownAnnotation = getAnnotation(EditorTypes.class); EditorTypes annotation = (ownAnnotation == null) ? type.getAnnotation(EditorTypes.class) : ownAnnotation; if (annotation != null) { addSubtypes(annotation, types); } // get the config key and add the config types String key = (annotation == null) ? type.getName() : annotation.key(); if (StringUtil.isBlank(key)) { if (annotation == ownAnnotation) { Member member = getMember(); key = member.getDeclaringClass().getName() + "." + member.getName(); } else { key = type.getName(); } } Class<?>[] ctypes = _configTypes.get(key); if (ctypes != null) { Collections.addAll(types, ctypes); } // if we don't have at least one non-null class, add the type itself if (types.size() == (nullable ? 1 : 0)) { types.add(type); } // convert to array, return return types.toArray(new Class<?>[types.size()]); }
/** * Returns a file from which the specified resource can be loaded. This method will unpack the * resource into a temporary directory and return a reference to that file. * * @param path the path to the resource in this jar file. * @return a file from which the resource can be loaded or null if no such resource exists. */ public File getResourceFile(String path) throws IOException { if (resolveJarFile()) { return null; } // if we have been unpacked, return our unpacked file if (_cache != null) { File cfile = new File(_cache, path); if (cfile.exists()) { return cfile; } else { return null; } } // otherwise, we unpack resources as needed into a temp directory String tpath = StringUtil.md5hex(_source.getPath() + "%" + path); File tfile = new File(getCacheDir(), tpath); if (tfile.exists() && (tfile.lastModified() > _sourceLastMod)) { return tfile; } JarEntry entry = _jarSource.getJarEntry(path); if (entry == null) { // log.info("Couldn't locate path in jar", "path", path, "jar", _jarSource); return null; } // copy the resource into the temporary file BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(tfile)); InputStream jin = _jarSource.getInputStream(entry); StreamUtil.copy(jin, fout); jin.close(); fout.close(); return tfile; }
/** Processes a resolved distributed object class instance. */ private static void processObject(File source, Class oclass) { if (!dObjectClass.isAssignableFrom(oclass) || dObjectClass.equals(oclass)) return; ArrayList<Field> flist = new ArrayList<Field>(); Field fields[] = oclass.getDeclaredFields(); for (Field f : fields) { int mods = f.getModifiers(); if (Modifier.isPublic(mods) && !Modifier.isStatic(mods) && !Modifier.isTransient(mods)) flist.add(f); } SourceFile sfile = new SourceFile(); try { sfile.readFrom(source); } catch (IOException ioe) { System.err.println( new StringBuilder() .append("Error reading '") .append(source) .append("': ") .append(ioe) .toString()); return; } StringBuilder fsection = new StringBuilder(); StringBuilder msection = new StringBuilder(); for (int ii = 0; ii < flist.size(); ii++) { Field f = flist.get(ii); Class ftype = f.getType(); String fname = f.getName(); VelocityContext ctx = new VelocityContext(); ctx.put("field", fname); ctx.put("type", com.samskivert.util.GenUtil.simpleName(f)); ctx.put("wrapfield", GenUtil.boxArgument(ftype, "value")); ctx.put("wrapofield", GenUtil.boxArgument(ftype, "ovalue")); ctx.put("clonefield", GenUtil.cloneArgument(dSetClass, f, "value")); ctx.put("capfield", StringUtil.unStudlyName(fname).toUpperCase()); ctx.put("upfield", StringUtils.capitalize(fname)); if (ftype.isArray()) { Class etype = ftype.getComponentType(); ctx.put("elemtype", com.samskivert.util.GenUtil.simpleName(etype, null)); ctx.put("wrapelem", GenUtil.boxArgument(etype, "value")); ctx.put("wrapoelem", GenUtil.boxArgument(etype, "ovalue")); } if (dSetClass.isAssignableFrom(ftype)) { java.lang.reflect.Type t; for (t = f.getGenericType(); t instanceof Class; t = ((Class) t).getGenericSuperclass()) ; if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; if (pt.getActualTypeArguments().length > 0) ctx.put( "etype", com.samskivert.util.GenUtil.simpleName( (Class) pt.getActualTypeArguments()[0], null)); } else { ctx.put("etype", "DSet.Entry"); } } String tname = "field.tmpl"; if (dSetClass.isAssignableFrom(ftype)) tname = "set.tmpl"; else if (oidListClass.isAssignableFrom(ftype)) tname = "oidlist.tmpl"; StringWriter fwriter = new StringWriter(); StringWriter mwriter = new StringWriter(); try { velocityEngine.mergeTemplate( "com/threerings/presents/tools/dobject_name.tmpl", "UTF-8", ctx, fwriter); velocityEngine.mergeTemplate( new StringBuilder() .append("com/threerings/presents/tools/dobject_") .append(tname) .toString(), "UTF-8", ctx, mwriter); } catch (Exception e) { System.err.println("Failed processing template"); e.printStackTrace(System.err); } if (ii > 0) { fsection.append("\n"); msection.append("\n"); } fsection.append(fwriter.toString()); msection.append(mwriter.toString()); } try { sfile.writeTo(source, fsection.toString(), msection.toString()); } catch (IOException ioe) { System.err.println( new StringBuilder() .append("Error writing '") .append(source) .append("': ") .append(ioe) .toString()); } }
@Override public String toString() { return StringUtil.fieldsToString(this); }
protected static void dumpTable(String prefix, Map<?, ?> table) { if (table != null) { System.out.println(prefix + StringUtil.toString(table.entrySet().iterator())); } }