private String getString(Map<String, String> resource, String key, Object[] params) { if (resource == null) { return "<" + key + ">"; } String value = resource.get(key); if (value == null) { value = "<" + key + ">"; } if (params != null) { try { value = String.format(value, params); } catch (FormatterClosedException fce) { LOG.log(Level.WARNING, fce.getMessage(), fce); value = "<" + key + ">"; } } return value; }
private static String composeHeapStatus(final long... memoryStatus) { final StringBuilder strBldr = new StringBuilder(); strBldr.append("%d M of %d M"); //NOPMD String output = Constants.EMPTY; //NOPMD try (final Formatter formatter = new Formatter(Locale.UK);) { formatter.format(strBldr.toString(), memoryStatus[0], memoryStatus[1]); //NOPMD output = formatter.toString(); //NOPMD } catch (IllegalFormatException | FormatterClosedException e) { LoggerFactory.getLogger(HeapStatusSimpleProvider.class).error("Error while composing Heap status ", e); //NOPMD } return output; }
/** * @tests java.util.Formatter#flush() */ public void test_flush() throws IOException { Formatter f = null; f = new Formatter(notExist); assertTrue(f instanceof Flushable); f.close(); try { f.flush(); fail("should throw FormatterClosedException"); } catch (FormatterClosedException e) { // expected } f = new Formatter(); // For destination that does not implement Flushable // No exception should be thrown f.flush(); }
public static void addRecords() { Scanner input = new Scanner(System.in); System.out.printf("%s%n%s%n? ", "Enter account number, first name, last name and balance.", "Enter end-of-file indicator to end input."); while (input.hasNext()) // loop until end-of-file indicator { try { // output new record to file; assumes valid input output.format("%d %s %s %.2f%n", input.nextInt(), input.next(), input.next(), input.nextDouble()); } catch (FormatterClosedException formatterClosedException) { System.err.println("Error writing to file. Terminating."); break; } catch (NoSuchElementException elementException) { System.err.println("Invalid input. Please try again."); input.nextLine(); // discard input so user can try again } System.out.print("? "); } }
private static String getString(Map<String, String> resource, String key, Object[] params) { String value = resource.get(key); if (value == null) { value = "<" + key + ">"; } if (params != null) { try { value = String.format(value, params); } catch (FormatterClosedException fce) { LOG.log(Level.WARNING, fce.getMessage(), fce); value = "<" + key + ">"; } } return value; }
/** * @tests java.util.Formatter#locale() */ public void test_locale() { Formatter f = null; f = new Formatter((Locale) null); assertNull(f.locale()); f.close(); try { f.locale(); fail("should throw FormatterClosedException"); } catch (FormatterClosedException e) { // expected } }
/** * @tests java.util.Formatter#out() */ public void test_out() { Formatter f = null; f = new Formatter(); assertNotNull(f.out()); assertTrue(f.out() instanceof StringBuilder); f.close(); try { f.out(); fail("should throw FormatterClosedException"); } catch (FormatterClosedException e) { // expected } }
/** * @tests java.util.Formatter#toString() */ public void test_toString() { Formatter f = new Formatter(); assertNotNull(f.toString()); assertEquals(f.out().toString(), f.toString()); f.close(); try { f.toString(); fail("should throw FormatterClosedException"); } catch (FormatterClosedException e) { // expected } }
private void ensureOpen() { if (a == null) throw new FormatterClosedException(); }
/** * @tests serialization/deserialization. */ public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new FormatterClosedException()); }
/** * @tests serialization/deserialization compatibility with RI. */ public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new FormatterClosedException()); }
public void addRecords() { // object to be written to file AccountRecord record = new AccountRecord(); Scanner input = new Scanner( System.in ); System.out.printf( "%s\n%s\n%s\n%s\n\n", "To terminate input, type the end-of-file indicator ", "when you are prompted to enter input.", "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter", "On Windows type <ctrl> z then press Enter" ); System.out.printf( "%s\n%s", "Enter account number (> 0), first name, last name and balance.", "? " ); while ( input.hasNext() ) // loop until end-of-file indicator { try // output values to file { // retrieve data to be output record.setAccount( input.nextInt() ); // read account number record.setFirstName( input.next() ); // read first name record.setLastName( input.next() ); // read last name record.setBalance( input.nextDouble() ); // read balance if ( record.getAccount() > 0 ) { // write new record output.format( "%d %s %s %.2f\n", record.getAccount(), record.getFirstName(), record.getLastName(), record.getBalance() ); } // end if else { System.out.println( "Account number must be greater than 0." ); } // end else } // end try catch ( FormatterClosedException formatterClosedException ) { System.err.println( "Error writing to file." ); return; } // end catch catch ( NoSuchElementException elementException ) { System.err.println( "Invalid input. Please try again." ); input.nextLine(); // discard input so user can try again } // end catch System.out.printf( "%s %s\n%s", "Enter account number (>0),", "first name, last name and balance.", "? " ); } // end while }