下面列出了java.util.Formatter#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public synchronized void logAndClear() {
if (payloadIdMissingMessagesMap.isEmpty()) {
return;
}
final StringBuilder stringBuilder = new StringBuilder();
final Formatter formatter = new Formatter(stringBuilder);
formatter.format("%n%1$31s%n%n", "MISSING PAYLOADS");
formatter.format("%1$19s | %2$8s | %3$47s %n", "payloadId", "retained", "topic");
formatter.format("%1$s%n", bigLine);
for (final Map.Entry<Long, MissingMessageInformation> entry : payloadIdMissingMessagesMap.entrySet()) {
final MissingMessageInformation missingMessage = entry.getValue();
formatter.format("%1$19d | %2$8b | %3$47s %n", missingMessage.getPayloadId(), missingMessage.isRetained(), missingMessage.getTopic());
formatter.format("%n%1$s%n", smallLine);
}
formatter.flush();
migrationLog.warn(stringBuilder.toString());
payloadIdMissingMessagesMap.clear();
}
/**
* print the file header, with the node name the raw data and the feature field
* @param out stream where write the feature data
* @param f feature that this close will dump
*/
protected void printHeader(Formatter out, Feature f) {
Field[] fields = f.getFieldsDesc();
out.format("Log start on," + DATE_FORMAT_TO_HEADER.format(mStartLog) + "\n");
out.format("Feature," + f.getName() + "\n");
out.format("Nodes,");
if (mNodeList != null)
for(Node n: mNodeList)
out.format(n.getFriendlyName() + ", ");
out.format("\n");
out.format(HOST_DATE_COLUMN+","+HOST_TIMESTAMP_COLUMN + " (ms)," + NODE_NAME_COLUMN + "," + NODE_TIMESTAMP_COLUMN + "," +
"" + NODE_RAW_DATA_COLUMN + ",");
for(Field field:fields){
out.format(field.getName());
String unit =field.getUnit();
if (unit!=null && !unit.isEmpty() )
out.format(" (%s)", field.getUnit());
out.format(",");
}//for
out.format("\n");
out.flush();
}
@Override
public String format(ValidationEvent event) {
StringWriter writer = new StringWriter();
Formatter formatter = new Formatter(writer);
formatter.format("%s: %s (%s)%n",
event.getSeverity(),
event.getShapeId().map(ShapeId::toString).orElse("-"),
event.getEventId());
if (event.getSourceLocation() != SourceLocation.NONE) {
String humanReadableFilename = getHumanReadableFilename(event.getSourceLocation());
String contextualLine = null;
try {
contextualLine = loadContextualLine(event.getSourceLocation());
} catch (IOException e) {
// Do nothing.
}
if (contextualLine == null) {
formatter.format(" @ %s%n", event.getSourceLocation());
} else {
// Show the filename.
formatter.format(" @ %s%n", humanReadableFilename);
formatter.format(" |%n");
// Show the line number and source code line.
formatter.format("%4d | %s%n", event.getSourceLocation().getLine(), contextualLine);
// Add a carat to point to the column of the error.
formatter.format(" | %" + event.getSourceLocation().getColumn() + "s%n", "^");
}
}
// Add the message and indent each line.
formatter.format(" = %s%n", event.getMessage().replace("\n", " \n"));
// Close up the formatter.
formatter.flush();
return writer.toString();
}
CodeTableGen(String filename) throws IOException {
out = new Formatter(System.out);
dataIS = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
getNextLine();
int count = 0;
while (readCodeTable() && count < 100) {
out.format("%d ", count);
count++;
}
out.flush();
dataIS.close();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Formatter f = new Formatter(sb, Locale.getDefault());
f.format("Classes:%n");
f.format("%s%n", classes);
f.format("FieldRefs:%n");
f.format("%s%n", fieldRefs);
f.format("MethodRefs:%n");
f.format("%s%n", methodRefs);
f.format("InterfaceMethodRefs:%n");
f.format("%s%n", intfMethodRefs);
f.flush();
return sb.toString();
}
private static void callWithFormatter(Closure closure, Formatter formatter) {
try {
closure.call(formatter);
} finally {
formatter.flush();
formatter.close();
}
}
/**
* close all the open file
*/
public void closeFiles(){
for (Formatter w : mFormatterCacheMap.values()) {
synchronized (w) {
w.flush();
w.close();
}
}//for
mFormatterCacheMap.clear();
}
/**
* Entry method to get the log. Lots of options
*
* @param maxEntries
* Max number of matching entries to fetch
* @param skip
* Number of entries to skip
* @param logLevel
* The minimum level
* @param reverse
* Return a reversed list or not
* @param noExceptions
* Do not print exceptions
* @param style
* Different print styles
* @return a formatted list
*/
public List<String> log(int maxEntries, int skip, Level logLevel, boolean reverse, boolean noExceptions,
Style style) {
List<LE> selected = select(maxEntries, skip, logLevel, reverse);
List<String> result = new ArrayList<String>(selected.size());
StringBuilder sb = new StringBuilder();
Formatter f = new Formatter(sb);
try {
for (LE entry : selected) {
//
// Clear the buffer, we reuse one buffer for efficiency
//
sb.setLength(0);
switch (style) {
case abbr:
abbr(f, entry, noExceptions);
break;
default:
classic(f, entry, noExceptions);
break;
}
f.flush();
result.add(sb.toString());
}
} finally {
f.close();
}
return result;
}
@Override
public void formatElement(StringWriter writer, ModelElementInstance element) {
Formatter formatter = new Formatter(writer);
if(element instanceof FlyingAnimal) {
formatter.format("%s\n", ((FlyingAnimal)element).getId());
}
else {
formatter.format("%s\n", element.getElementType().getTypeName());
}
formatter.flush();
}
private static void printArray(Formatter out, Array array, String name, Indent indent, CancelTask ct) {
printArray(out, array, name, null, indent, ct, true);
out.flush();
}
private static void printArray(Formatter out, Array array, String name, String units, Indent ilev, CancelTask ct,
boolean printSeq) {
if (ct != null && ct.isCancel())
return;
if (name != null)
out.format("%s%s = ", ilev, name);
ilev.incr();
if (array == null) {
out.format("null array for %s", name);
ilev.decr();
return;
}
if ((array instanceof ArrayChar) && (array.getRank() > 0)) {
printStringArray(out, (ArrayChar) array, ilev, ct);
} else if (array.getElementType() == String.class) {
printStringArray(out, array, ilev, ct);
} else if (array instanceof ArraySequence) {
if (printSeq)
printSequence(out, (ArraySequence) array, ilev, ct);
} else if (array instanceof ArrayStructure) {
printStructureDataArray(out, (ArrayStructure) array, ilev, ct);
} else if (array.getElementType() == ByteBuffer.class) { // opaque type
array.resetLocalIterator();
while (array.hasNext()) {
printByteBuffer(out, (ByteBuffer) array.next(), ilev);
out.format("%s%n", array.hasNext() ? "," : ";"); // peek ahead
if (ct != null && ct.isCancel())
return;
}
} else if (array instanceof ArrayObject) {
printVariableArray(out, (ArrayObject) array, ilev, ct);
} else {
printArray(out, array, ilev, ct);
}
if (units != null)
out.format(" %s", units);
out.format("%n");
ilev.decr();
out.flush();
}
/**
* Writes the model to the modelOutput.
* It uses {@link java.util.Locale#ENGLISH} for number formatting.
*
* <p><b>Note: The modelOutput is closed after reading or in case of an exception.</b></p>
*/
public static void saveModel(Writer modelOutput, Model model) throws IOException {
int nr_feature = model.nr_feature;
int w_size = nr_feature;
if (model.bias >= 0) w_size++;
int nr_w = model.nr_class;
if (model.nr_class == 2 && model.solverType != SolverType.MCSVM_CS) nr_w = 1;
Formatter formatter = new Formatter(modelOutput, DEFAULT_LOCALE);
try {
printf(formatter, "solver_type %s\n", model.solverType.name());
printf(formatter, "nr_class %d\n", model.nr_class);
if (model.label != null) {
printf(formatter, "label");
for (int i = 0; i < model.nr_class; i++) {
printf(formatter, " %d", model.label[i]);
}
printf(formatter, "\n");
}
printf(formatter, "nr_feature %d\n", nr_feature);
printf(formatter, "bias %.16g\n", model.bias);
printf(formatter, "w\n");
for (int i = 0; i < w_size; i++) {
for (int j = 0; j < nr_w; j++) {
double value = model.w[i * nr_w + j];
/** this optimization is the reason for {@link Model#equals(double[], double[])} */
if (value == 0.0) {
printf(formatter, "%d ", 0);
} else {
printf(formatter, "%.16g ", value);
}
}
printf(formatter, "\n");
}
formatter.flush();
IOException ioException = formatter.ioException();
if (ioException != null) throw ioException;
}
finally {
formatter.close();
}
}
/**
* Writes the model to the modelOutput. It uses {@link java.util.Locale#ENGLISH} for number
* formatting.
*
* <p>
* <b>Note: The modelOutput is closed after reading or in case of an exception.</b>
* </p>
*/
public static void saveModel(Writer modelOutput, Model model) throws IOException {
int nr_feature = model.nr_feature;
int w_size = nr_feature;
if (model.bias >= 0) {
w_size++;
}
int nr_w = model.nr_class;
if (model.nr_class == 2 && model.solverType != SolverType.MCSVM_CS) {
nr_w = 1;
}
Formatter formatter = new Formatter(modelOutput, DEFAULT_LOCALE);
try {
printf(formatter, "solver_type %s\n", model.solverType.name());
printf(formatter, "nr_class %d\n", model.nr_class);
if (model.label != null) {
printf(formatter, "label");
for (int i = 0; i < model.nr_class; i++) {
printf(formatter, " %d", model.label[i]);
}
printf(formatter, "\n");
}
printf(formatter, "nr_feature %d\n", nr_feature);
printf(formatter, "bias %.16g\n", model.bias);
printf(formatter, "w\n");
for (int i = 0; i < w_size; i++) {
for (int j = 0; j < nr_w; j++) {
double value = model.w[i * nr_w + j];
/** this optimization is the reason for {@link Model#equals(double[], double[])} */
if (value == 0.0) {
printf(formatter, "%d ", 0);
} else {
printf(formatter, "%.16g ", value);
}
}
printf(formatter, "\n");
}
formatter.flush();
IOException ioException = formatter.ioException();
if (ioException != null) {
throw ioException;
}
} finally {
formatter.close();
}
}
/**
* Format the commit counter with leading zeros such that it will be
* lexically ordered in the file system.
*
* @param commitCounter
* The commit counter.
*
* @return The basename of the file consisting of the foramtted commit
* counter with the appropriate leading zeros.
*/
public static String getCommitCounterStr(final long commitCounter) {
final StringBuilder sb = new StringBuilder(BASENAME_DIGITS);
final Formatter f = new Formatter(sb);
f.format(FORMAT_STR, commitCounter);
f.flush();
f.close();
final String basename = sb.toString();
return basename;
}
/**
* Writes the model to the modelOutput.
* It uses {@link java.util.Locale#ENGLISH} for number formatting.
*
* <p><b>Note: The modelOutput is closed after reading or in case of an exception.</b></p>
*/
public static void saveModel(Writer modelOutput, Model model) throws IOException {
int nr_feature = model.nr_feature;
int w_size = nr_feature;
if (model.bias >= 0) w_size++;
int nr_w = model.nr_class;
if (model.nr_class == 2 && model.solverType != SolverType.MCSVM_CS) nr_w = 1;
Formatter formatter = new Formatter(modelOutput, DEFAULT_LOCALE);
try {
printf(formatter, "solver_type %s\n", model.solverType.name());
printf(formatter, "nr_class %d\n", model.nr_class);
printf(formatter, "label");
for (int i = 0; i < model.nr_class; i++) {
printf(formatter, " %d", model.label[i]);
}
printf(formatter, "\n");
printf(formatter, "nr_feature %d\n", nr_feature);
printf(formatter, "bias %.16g\n", model.bias);
printf(formatter, "w\n");
for (int i = 0; i < w_size; i++) {
for (int j = 0; j < nr_w; j++) {
double value = model.w[i * nr_w + j];
/** this optimization is the reason for {@link Model#equals(double[], double[])} */
if (value == 0.0) {
printf(formatter, "%d ", 0);
} else {
printf(formatter, "%.16g ", value);
}
}
printf(formatter, "\n");
}
formatter.flush();
IOException ioException = formatter.ioException();
if (ioException != null) throw ioException;
}
finally {
formatter.close();
}
}
/**
* Generates a property filter condition string for a <code>byte[]</code> value. Creates a formatted string to use
* in a filter expression that uses the specified operation to compare the property with the value, formatted as a
* binary value, as in the following example:
* <p>
* <code>String condition = generateFilterCondition("ByteArray", QueryComparisons.EQUAL, new byte[] {0x01, 0x0f});</code>
* <p>
* This statement sets <code>condition</code> to the following value:
* <p>
* <code>ByteArray eq X'010f'</code>
*
* @param propertyName
* A <code>String</code> which specifies the name of the property to compare.
* @param operation
* A <code>String</code> which specifies the comparison operator to use.
* @param value
* A <code>byte</code> array which specifies the value to compare with the property.
* @return
* A <code>String</code> which represents the formatted filter condition.
*/
public static String generateFilterCondition(String propertyName, String operation, final byte[] value) {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
for (byte b : value) {
formatter.format("%02x", b);
}
formatter.flush();
formatter.close();
return generateFilterCondition(propertyName, operation, sb.toString(), EdmType.BINARY);
}
/**
* Generates a property filter condition string for a <code>Byte[]</code> value. Creates a formatted string to use
* in a filter expression that uses the specified operation to compare the property with the value, formatted as a
* binary value, as in the following example:
* <p>
* <code>String condition = generateFilterCondition("ByteArray", QueryComparisons.EQUAL, new Byte[] {0x01, 0xfe});</code>
* <p>
* This statement sets <code>condition</code> to the following value:
* <p>
* <code>ByteArray eq X'01fe'</code>
*
* @param propertyName
* A <code>String</code> which specifies the name of the property to compare.
* @param operation
* A <code>String</code> which specifies the comparison operator to use.
* @param value
* A <code>Byte</code> array which specifies the value to compare with the property.
* @return
* A <code>String</code> which represents the formatted filter condition.
*/
public static String generateFilterCondition(String propertyName, String operation, final Byte[] value) {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
for (byte b : value) {
formatter.format("%02x", b);
}
formatter.flush();
formatter.close();
return generateFilterCondition(propertyName, operation, sb.toString(), EdmType.BINARY);
}