List of usage examples for java.lang Character isLetter
public static boolean isLetter(int codePoint)
From source file:com.qmetry.qaf.automation.util.StringUtil.java
public static String getRandomString(String format) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < format.length(); i++) { char c = format.charAt(i); char a = Character.isDigit(c) ? RandomStringUtils.randomNumeric(1).charAt(0) : Character.isLetter(c) ? RandomStringUtils.randomAlphabetic(c).charAt(0) : c; sb.append(a);//from ww w. ja v a 2s . co m } return sb.toString(); }
From source file:edu.smc.mediacommons.panels.PasswordPanel.java
public PasswordPanel() { setLayout(null);/* w w w . ja v a2 s .com*/ add(Utils.createLabel("Enter a Password to Test", 60, 30, 200, 20, null)); final JButton test = Utils.createButton("Test", 260, 50, 100, 20, null); add(test); final JPasswordField fieldPassword = new JPasswordField(32); fieldPassword.setBounds(60, 50, 200, 20); add(fieldPassword); final PieDataset dataset = createSampleDataset(33, 33, 33); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(300, 300)); chartPanel.setBounds(45, 80, 340, 250); add(chartPanel); test.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String password = new String(fieldPassword.getPassword()); if (password.isEmpty() || password == null) { JOptionPane.showMessageDialog(getParent(), "Warning! The input was blank!", "Invalid Input", JOptionPane.ERROR_MESSAGE); } else { int letterCount = 0, numberCount = 0, specialCount = 0, total = password.length(); for (char c : password.toCharArray()) { if (Character.isLetter(c)) { letterCount++; } else if (Character.isDigit(c)) { numberCount++; } else { specialCount++; } } long totalCombinations = 0; double percentLetters = 0; double percentNumbers = 0; double percentCharacters = 0; if (letterCount > 0) { totalCombinations += (factorial(26) / factorial(26 - letterCount)); percentLetters = (letterCount + 0.0 / total); } if (numberCount > 0) { totalCombinations += (factorial(10) / factorial(10 - numberCount)); percentNumbers = (numberCount + 0.0 / total); } if (specialCount > 0) { totalCombinations += (factorial(40) / factorial(40 - specialCount)); percentCharacters = (specialCount + 0.0 / total); } PieDataset dataset = createSampleDataset(percentLetters, percentNumbers, percentCharacters); JFreeChart chart = createChart(dataset); chartPanel.setChart(chart); JOptionPane.showMessageDialog(getParent(), "Total Combinations: " + totalCombinations + "\nAssuming Rate Limited, Single: " + (totalCombinations / 1000) + " seconds" + "\n\nBreakdown:\nLetters: " + percentLetters + "%\nNumbers: " + percentNumbers + "%\nCharacters: " + percentCharacters + "%"); } } }); setVisible(true); }
From source file:edu.msu.cme.rdp.readseq.utils.SequenceTrimmer.java
public static TrimStats getStats(Sequence seq, int trimStart, int trimStop) { if (trimStart >= trimStop) { throw new IllegalArgumentException("Trim start has to come before trim end"); }//from ww w . ja va 2 s . com TrimStats ret = new TrimStats(); int modelPos = 0; int seqPos = 0; int index; int filled = 0; char b; boolean ischar, isgap, ismodel, isupper, intrim; int alnStart = -1; char[] bases = seq.getSeqString().toCharArray(); for (index = 0; index < bases.length; index++) { b = bases[index]; ischar = Character.isLetter(b); isupper = ischar && Character.isUpperCase(b); isgap = !ischar && (b == '.' || b == '~' || b == '-'); ismodel = (b == '-') || isupper; intrim = (modelPos >= trimStart && modelPos < trimStop); if (ischar) { seqPos++; if (b == 'n' || b == 'N') { ret.numNs++; if (intrim) { ret.nsInTrim++; } } if (intrim) { ret.trimmedLength++; } } if (ismodel) { if (modelPos == trimStart) { ret.seqTrimStart = seqPos; alnStart = index; } if (modelPos >= trimStart && modelPos < trimStop) { filled++; if (ischar) { ret.filledRatio++; } } if (modelPos == trimStop) { ret.seqTrimStop = seqPos; ret.trimmedBases = Arrays.copyOfRange(bases, alnStart, index); } if (!isgap) { if (ret.seqStart == -1) { ret.seqStart = modelPos; } ret.seqStop = modelPos; } modelPos++; } } ret.seqLength = seqPos; ret.modelLength = modelPos; ret.filledRatio /= filled; return ret; }
From source file:org.openmrs.web.WebUtil.java
/** * This method checks if input locale string contains control characters and tries to clean up * actually contained ones. Also it parses locale object from string representation and * validates it object./* ww w . ja v a 2s.co m*/ * * @param localeString input string with locale parameter * @return locale object for input string if CTLs were cleaned up or weren't exist or null if * could not to clean up CTLs from input string * @should ignore leading spaces * @should accept language only locales * @should not accept invalid locales * @should not fail with empty strings * @should not fail with whitespace only */ public static Locale normalizeLocale(String localeString) { if (localeString == null) { return null; } localeString = localeString.trim(); if (localeString.isEmpty()) { return null; } int len = localeString.length(); for (int i = 0; i < len; i++) { char c = localeString.charAt(i); // allow only ASCII letters and "_" character if ((c <= 0x20 || c >= 0x7f) || ((c >= 0x20 || c <= 0x7f) && (!Character.isLetter(c) && c != 0x5f))) { if (c == 0x09) { continue; // allow horizontal tabs } localeString = localeString.replaceFirst(((Character) c).toString(), ""); len--; i--; } } Locale locale = LocaleUtility.fromSpecification(localeString); if (LocaleUtility.isValid(locale)) { return locale; } else { return null; } }
From source file:com.sjsu.crawler.util.RandomStringUtilsTest.java
/** * Test the implementation//from ww w .ja v a 2 s . co m */ @Test public void testRandomStringUtils() { String r1 = RandomStringUtils.random(50); assertEquals("random(50) length", 50, r1.length()); String r2 = RandomStringUtils.random(50); assertEquals("random(50) length", 50, r2.length()); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomAscii(50); assertEquals("randomAscii(50) length", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("char between 32 and 127", r1.charAt(i) >= 32 && r1.charAt(i) <= 127); } r2 = RandomStringUtils.randomAscii(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomAlphabetic(50); assertEquals("randomAlphabetic(50)", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("r1 contains alphabetic", Character.isLetter(r1.charAt(i)) && !Character.isDigit(r1.charAt(i))); } r2 = RandomStringUtils.randomAlphabetic(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomAlphanumeric(50); assertEquals("randomAlphanumeric(50)", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("r1 contains alphanumeric", Character.isLetterOrDigit(r1.charAt(i))); } r2 = RandomStringUtils.randomAlphabetic(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomNumeric(50); assertEquals("randomNumeric(50)", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("r1 contains numeric", Character.isDigit(r1.charAt(i)) && !Character.isLetter(r1.charAt(i))); } r2 = RandomStringUtils.randomNumeric(50); assertTrue("!r1.equals(r2)", !r1.equals(r2)); String set = "abcdefg"; r1 = RandomStringUtils.random(50, set); assertEquals("random(50, \"abcdefg\")", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("random char in set", set.indexOf(r1.charAt(i)) > -1); } r2 = RandomStringUtils.random(50, set); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.random(50, (String) null); assertEquals("random(50) length", 50, r1.length()); r2 = RandomStringUtils.random(50, (String) null); assertEquals("random(50) length", 50, r2.length()); assertTrue("!r1.equals(r2)", !r1.equals(r2)); set = "stuvwxyz"; r1 = RandomStringUtils.random(50, set.toCharArray()); assertEquals("random(50, \"stuvwxyz\")", 50, r1.length()); for (int i = 0; i < r1.length(); i++) { assertTrue("random char in set", set.indexOf(r1.charAt(i)) > -1); } r2 = RandomStringUtils.random(50, set); assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.random(50, (char[]) null); assertEquals("random(50) length", 50, r1.length()); r2 = RandomStringUtils.random(50, (char[]) null); assertEquals("random(50) length", 50, r2.length()); assertTrue("!r1.equals(r2)", !r1.equals(r2)); final long seed = System.currentTimeMillis(); r1 = RandomStringUtils.random(50, 0, 0, true, true, null, new Random(seed)); r2 = RandomStringUtils.random(50, 0, 0, true, true, null, new Random(seed)); assertEquals("r1.equals(r2)", r1, r2); r1 = RandomStringUtils.random(0); assertEquals("random(0).equals(\"\")", "", r1); }
From source file:com.twitter.hraven.etl.JobHistoryFileParserBase.java
/** * parses the -Xmx value from the mapred.child.java.opts * in the job conf usually appears as the * following in the job conf://from w w w. j ava2s . co m * "mapred.child.java.opts" : "-Xmx3072M" * or * "mapred.child.java.opts" :" -Xmx1024m -verbose:gc -Xloggc:/tmp/@taskid@.gc * @return xmx value in MB */ public static long getXmxValue(String javaChildOptsStr) { long retVal = 0L; String valueStr = extractXmxValueStr(javaChildOptsStr); char lastChar = valueStr.charAt(valueStr.length() - 1); try { if (Character.isLetter(lastChar)) { String xmxValStr = valueStr.substring(0, valueStr.length() - 1); retVal = Long.parseLong(xmxValStr); switch (lastChar) { case 'M': case 'm': // do nothing, since it's already in megabytes break; case 'K': case 'k': // convert kilobytes to megabytes retVal /= 1024; break; case 'G': case 'g': // convert gigabytes to megabtyes retVal *= 1024; break; default: throw new ProcessingException("Unable to get the Xmx value from " + javaChildOptsStr + " invalid value for Xmx " + xmxValStr); } } else { retVal = Long.parseLong(valueStr); // now convert to megabytes // since this was in bytes since the last char was absent retVal /= (1024 * 1024); } } catch (NumberFormatException nfe) { LOG.error("Unable to get the Xmx value from " + javaChildOptsStr + "\n", nfe); throw new ProcessingException("Unable to get the Xmx value from " + javaChildOptsStr, nfe); } return retVal; }
From source file:jshm.sh.scraper.wiki.ActionsScraper.java
public static Map<String, List<Action>> scrape(final Reader reader, final Map<String, List<Action>> ret) throws IOException, ScraperException { if (null == ret) throw new NullPointerException("ret"); BufferedReader in = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader); StringBuilder sb = null;//from ww w. j a va2s . co m String lastKey = null; Action action = null; int c = -1, leftBraceCount = 0, rightBraceCount = 0; boolean isQuotedString = false; Expect expect = Expect.LEFT_BRACE; while (-1 != (c = in.read())) { switch (expect) { case LEFT_BRACE: if ('{' == c) { leftBraceCount++; if (2 == leftBraceCount) { LOG.finer("got 2 left braces, expecting name"); expect = Expect.NAME; action = new Action(); sb = new StringBuilder(); isQuotedString = false; } } // skipping non action block continue; case NAME: if (Character.isLetter(c)) { sb.append((char) c); continue; } else if ('}' == c) { // no key/value pairs rightBraceCount++; action.name = sb.toString(); if (null == ret.get(action.name)) ret.put(action.name, new ArrayList<Action>()); ret.get(action.name).add(action); expect = Expect.RIGHT_BRACE; LOG.finer("got right brace after name (" + action.name + "), action has no args"); continue; } else if (Character.isWhitespace(c)) { // we've read some characters for the name if (0 != sb.length()) { action.name = sb.toString(); if (null == ret.get(action.name)) ret.put(action.name, new ArrayList<Action>()); ret.get(action.name).add(action); expect = Expect.KEY; sb = new StringBuilder(); isQuotedString = false; LOG.finer("got whitespace after name (" + action.name + ")"); } // else there's whitespace before the name continue; } throw new ScraperException("expecting next letter in name or whitespace after, got: " + (char) c); case KEY: if (Character.isLetter(c)) { sb.append((char) c); continue; } else if (Character.isWhitespace(c) && 0 == sb.length()) { // extra whitespace between last thing and this key continue; } else if ('}' == c) { if (0 != sb.length()) throw new ScraperException("expecting next letter in key but got right brace"); LOG.finer("got right brace after last value"); rightBraceCount++; expect = Expect.RIGHT_BRACE; continue; } else if ('=' == c) { lastKey = sb.toString(); LOG.finer("got equals sign after key (" + lastKey + ")"); expect = Expect.VALUE; sb = new StringBuilder(); isQuotedString = false; continue; } throw new ScraperException("expecting next letter in key or equals sign, got: " + (char) c); case VALUE: if (isQuotedString && '"' != c && '}' != c) { sb.append((char) c); continue; } else if ('"' == c || (isQuotedString && '}' == c)) { if ('}' == c) { // malformed, no end quote isQuotedString = false; } else { isQuotedString = !isQuotedString; } if (isQuotedString) { LOG.finest("got opening quote for value of key (" + lastKey + ")"); } else { String value = org.htmlparser.util.Translate.decode(sb.toString().trim()); LOG.finer("got closing quote for value (" + value + ")"); action.args.put(lastKey, value); if ('}' == c) { expect = Expect.RIGHT_BRACE; rightBraceCount++; } else { expect = Expect.KEY; } lastKey = null; sb = new StringBuilder(); isQuotedString = false; } continue; } else if (Character.isWhitespace(c)) { LOG.finer("got whitespace before value for key (" + lastKey + ")"); expect = Expect.KEY; lastKey = null; sb = new StringBuilder(); isQuotedString = false; continue; } throw new ScraperException("expecting quote or next letter in value, got: " + (char) c); case RIGHT_BRACE: if ('}' == c) { rightBraceCount++; if (2 == rightBraceCount) { LOG.finer("got 2nd right brace"); leftBraceCount = rightBraceCount = 0; expect = Expect.LEFT_BRACE; continue; } } throw new ScraperException("expecting 2nd right brace, got " + (char) c); } } return ret; }
From source file:com.github.tteofili.p2h.DataSplitTest.java
private boolean isValid(String string) { boolean result = false; char[] chars = string.toCharArray(); for (char aChar : chars) { if (result = Character.isLetter(aChar)) { break; }//from w ww . j a v a 2s. c om } return result; }
From source file:importer.filters.PlayFilter.java
/** * Remove non-letter and hyphen chars from the string * @param input the raw punctuated word/* w w w .j a v a2 s . com*/ * @return a punctuation-free word (but with hyphens) */ String strip(String input) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.length(); i++) { char token = input.charAt(i); if (Character.isLetter(token) || token == '-') sb.append(token); } return sb.toString(); }
From source file:org.openlegacy.utils.StringUtil.java
public static String toVariableName(String text, boolean capFirst) { String variableName = null;//from w w w .j ava 2 s . co m if (!text.contains(" ") && !text.contains("_") && !text.contains("-")) { text = text.replaceAll("\\W", ""); if (!capFirst) { variableName = StringUtils.uncapitalize(text); } else { variableName = StringUtils.capitalize(text); } } else { char[] chars = text.toCharArray(); StringBuilder sb = new StringBuilder(text.length()); for (char d : chars) { char c = d; if (capFirst) { c = Character.toUpperCase(c); capFirst = false; } else { c = Character.toLowerCase(c); } if (Character.isLetter(c) || Character.isDigit(c)) { sb.append(c); } if (c == ' ' || c == '_' || c == '-') { capFirst = true; } } variableName = sb.toString(); } if (RESERVERD_WORDS_DICTIONARY.containsKey(variableName)) { variableName = variableName + "_"; } if (!StringUtils.isEmpty(variableName) && variableName.charAt(0) >= '0' && variableName.charAt(0) <= '9') { variableName = "_" + variableName; } return variableName; }