List of usage examples for java.lang Character isDigit
public static boolean isDigit(int codePoint)
From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java
private static boolean isNumeric(String str) { for (char c : str.toCharArray()) { if (!Character.isDigit(c)) { return false; }//from ww w . j a va2s . co m } return true; }
From source file:DateUtils.java
/** * Parse date time value from given string resolving any functions or formulas * the string can contain. This method can be therefore used if the passed * string contains string representation date, time or timestamp or a formula * such as now + 3h - 1m + 4d. /*from ww w .j av a2s . c om*/ * * @param strValue - string representation of date or date function * @param iDateType - date type code, one of the DATE_TYPE_XXX constants * @param stored - flag if Date should be parsed using format used for * storage or for display * @return Timestamp - parsed date or null if date was null * @throws OSSInvalidDataException - error during parsing */ public static Timestamp parseDateTime(String strValue, int iDateType, boolean stored) throws Exception { Timestamp tsReturn = null; Calendar workCal = GregorianCalendar.getInstance(); if (strValue != null && strValue.length() > 0) { strValue = strValue.trim(); if (strValue.startsWith(CURRENT_DATE_CODE)) { strValue = strValue.replaceAll("[ ]", ""); // If the user specified "UseCurrent", then substitute the // current date/time in the value workCal.setTime(new Date()); // Log.getInstance().debug("Parsing current date " + strValue); // Parse the date math int iBeginIndex = CURRENT_DATE_CODE.length(); int iMaxLength = strValue.length(); int iSign = 1; int iNumberIndex; int iValue; char cChar = ' '; while (iBeginIndex < iMaxLength) { // This has to be sign if (strValue.charAt(iBeginIndex) == '+') { iSign = 1; } else if (strValue.charAt(iBeginIndex) == '-') { iSign = -1; } else { // Incorrect String throw new Exception("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iBeginIndex)); } iBeginIndex++; // Now we have to have number iNumberIndex = iBeginIndex; while (((iBeginIndex == iNumberIndex) || Character.isDigit(cChar)) && (iBeginIndex < iMaxLength)) { cChar = strValue.charAt(iBeginIndex++); } // We have to go one back because we should stop on modifier (e.g 1m) iBeginIndex--; try { iValue = Integer.parseInt(strValue.substring(iNumberIndex, iBeginIndex)); } catch (NumberFormatException nmeExc) { // Incorrect String throw new Exception("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iNumberIndex)); } // This has to be modifier: y - year, M - month, w - week, // d - day, h - hour, m - minute, s - second cChar = strValue.charAt(iBeginIndex); switch (cChar) { case (YEAR_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used YEAR modifier for TIME type"); } workCal.add(Calendar.YEAR, iSign * iValue); break; } case (MONTH_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used MONTH modifier for TIME type"); } workCal.add(Calendar.MONTH, iSign * iValue); break; } case (WEEK_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used WEEK modifier for TIME type"); } workCal.add(Calendar.WEEK_OF_YEAR, iSign * iValue); break; } case (DAY_CODE): { if (iDateType == DATE_TYPE_TIME) { throw new Exception( "Date function is in incorrect format: " + "used DAY modifier for TIME type"); } workCal.add(Calendar.DATE, iSign * iValue); break; } case (HOUR_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new Exception( "Date function is in incorrect format: " + "used HOUR modifier for DATE type"); } workCal.add(Calendar.HOUR, iSign * iValue); break; } case (MINUTE_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new Exception("Date function is in incorrect format: " + "used MINUTE modifier for DATE type"); } workCal.add(Calendar.MINUTE, iSign * iValue); break; } case (SECOND_CODE): { if (iDateType == DATE_TYPE_DATE) { throw new Exception("Date function is in incorrect format: " + "used SECOND modifier for DATE type"); } workCal.add(Calendar.SECOND, iSign * iValue); break; } default: { // Incorrect String throw new Exception("Date function is in incorrect format: " + strValue + " at " + strValue.substring(iBeginIndex)); } } iBeginIndex++; } tsReturn = new Timestamp(workCal.getTimeInMillis()); } else { try { if (stored) { switch (iDateType) { case (DATE_TYPE_DATE): { tsReturn = new Timestamp(DATE_STORE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_TIME): { tsReturn = new Timestamp(TIME_STORE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_DATETIME): { tsReturn = new Timestamp(DATETIME_STORE_FORMAT.parse(strValue).getTime()); break; } default: { assert false : "Unknown date type " + iDateType; } } } else { switch (iDateType) { case (DATE_TYPE_DATE): { tsReturn = new Timestamp(DATE_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_TIME): { tsReturn = new Timestamp(TIME_FORMAT.parse(strValue).getTime()); break; } case (DATE_TYPE_DATETIME): { tsReturn = new Timestamp(DATETIME_FORMAT.parse(strValue).getTime()); break; } default: { assert false : "Unknown date type " + iDateType; } } } } catch (ParseException peExc) { throw new Exception("Date is in incorrect format. Problems with parsing.", peExc); } } } return tsReturn; }
From source file:com.architexa.diagrams.jdt.model.CodeUnit.java
private static boolean isAnonConstructor(Artifact art, ReloRdfRepository repo) { if (repo.hasStatement(art.elementRes, AsmMethodSupport.anonymousMethodType, true)) return true; String name = art.queryName(repo); if (name != null && !"".equals(name.trim()) && Character.isDigit(name.charAt(0))) return true; Artifact parentArt = art.queryParentArtifact(repo); if (!ErrorUtils.isValidType(parentArt, repo, true)) return false; if (!CodeUnit.isType(repo, parentArt.queryType(repo))) return false; String methodName = art.queryName(repo); String parentName = parentArt.queryName(repo); if (methodName == null || !methodName.equals(parentName)) return false; // should never reach this point and have to rely on IJavaElements IJavaElement parentClassElt = RJCore.resourceToJDTElement(repo, parentArt.elementRes); boolean isInAnonClass = (parentClassElt instanceof SourceType) && ((SourceType) parentClassElt).isAnonymous(); if (!isInAnonClass) return false; IJavaElement anonMethodElt = RJCore.resourceToJDTElement(repo, art.elementRes); try {/*ww w .ja v a 2 s . co m*/ return ((IMethod) anonMethodElt).isConstructor(); } catch (JavaModelException e) { logger.error( "Unexpected exception while testing whether method " + art.elementRes + " is constructor."); } return false; }
From source file:org.pentaho.chart.plugin.jfreechart.chart.dial.JFreeDialChartGenerator.java
/** * Gets the numeric value inside the CSS value; ignores units. *///from ww w. j a v a 2 s. c o m protected double parseDouble(final CSSValue value) { String trimmedString = value.getCSSText().trim(); for (int i = 0; i < trimmedString.length(); i++) { char c = trimmedString.charAt(i); if (!Character.isDigit(c)) { try { double d = Double.parseDouble(trimmedString.substring(0, i)); return d; } catch (NumberFormatException e) { return 0; } } } return 0; }
From source file:edu.stanford.muse.index.Indexer.java
private static boolean drop(String token) { if (commonDictWords.contains(token) || (token.length() > MAX_TOKEN_LENGTH)) return true; // try to parse for number only if term has a chance of being one // earlier we were trying to parseInt each term which is expensive for runtime // since it throws an exception per non-numeric term if (Character.isDigit(token.charAt(0))) { char[] chars = token.toCharArray(); for (char c : chars) if (Character.isDigit(c)) return true; }//from w w w . ja v a 2s. c o m return false; }
From source file:ca.mcgill.cs.swevo.qualyzer.editors.RTFDocumentProvider2.java
private ParserPair handleControl(InputStream contentStream, int startChar, String startControl, Map<String, Integer> state) throws IOException { StringBuilder controlWord = new StringBuilder(startControl); int c = startChar; char ch;/*from w w w .j av a 2s .co m*/ while (c != -1) { ch = (char) c; if (ch == UNICODE && isEmpty(controlWord)) { // This is potentially an unicode char ParserPair pair = getUnicode(contentStream, state); c = pair.fChar; controlWord = new StringBuilder(pair.fString); break; } else if (Character.isLetter(ch)) { // Start of a control word controlWord.append(ch); } else if (ch == ESCAPE_8BIT && isEmpty(controlWord)) { // This is an escaped 8bit char ParserPair pair = get8Bit(contentStream); c = pair.fChar; controlWord = new StringBuilder(pair.fString); break; } else if (Character.isDigit(ch)) { // Unit of control word controlWord.append(ch); } else if (ch == MINUS) { controlWord.append(ch); } else { if (isEmpty(controlWord)) { controlWord.append(ch); c = contentStream.read(); } else if (Character.isWhitespace(ch)) { // This is a delimiter. Skip it c = contentStream.read(); } break; } c = contentStream.read(); } return new ParserPair(c, controlWord.toString()); }
From source file:gdsc.smlm.ij.plugins.pcpalm.PCPALMClusters.java
/** * Load the histogram from the file. Assumes the histogram is [int, float] format and creates a contiguous histogram * from zero//from ww w . j a v a2s .co m * * @param filename * @return */ private HistogramData loadHistogram(String filename) { BufferedReader input = null; try { int f = 0; double a = 0; String u = ""; FileInputStream fis = new FileInputStream(filename); input = new BufferedReader(new UnicodeReader(fis, null)); String line; int count = 0; ArrayList<float[]> data = new ArrayList<float[]>(); // Read the header and store the calibration if present while ((line = input.readLine()) != null) { count++; if (line.length() == 0) continue; if (Character.isDigit(line.charAt(0))) // This is the first record break; String[] fields = line.split("[\t, ]+"); if (fields[0].equalsIgnoreCase("frames")) f = Integer.parseInt(fields[1]); if (fields[0].equalsIgnoreCase("area")) a = Double.parseDouble(fields[1]); if (fields[0].equalsIgnoreCase("units")) u = fields[1]; } final Pattern pattern = Pattern.compile("[\t, ]+"); while (line != null) { if (line.length() == 0) continue; if (!Character.isDigit(line.charAt(0))) continue; // Extract the first 2 fields Scanner scanner = new Scanner(line); scanner.useLocale(Locale.US); scanner.useDelimiter(pattern); try { int molecules = scanner.nextInt(); float frequency = scanner.nextFloat(); // Check for duplicates for (float[] d : data) { if (d[0] == molecules) { error("Duplicate molecules field on line " + count); return null; } } data.add(new float[] { molecules, frequency }); } catch (InputMismatchException e) { error("Incorrect fields on line " + count); return null; } catch (NoSuchElementException e) { error("Incorrect fields on line " + count); return null; } finally { scanner.close(); } // Get the next line line = input.readLine(); count++; } if (data.isEmpty()) { error("No data in file " + filename); return null; } // Create a contiguous histogram from zero int maxN = 0; for (float[] d : data) { if (maxN < d[0]) maxN = (int) d[0]; } float[][] hist = new float[2][maxN + 1]; for (int n = 0; n <= maxN; n++) { hist[0][n] = n; for (float[] d : data) { if (n == d[0]) hist[1][n] = d[1]; } } HistogramData histogramData = new HistogramData(hist, f, a, u); histogramData.filename = filename; return histogramData; } catch (IOException e) { IJ.error(TITLE, "Unable to read from file " + filename); } finally { try { if (input != null) input.close(); } catch (IOException e) { // Ignore } } return null; }
From source file:org.mcxiaoke.commons.http.util.URIUtilsEx.java
/** * Extracts target host from the given {@link URI}. * //from ww w. jav a 2 s .c o m * @param uri * @return the target host if the URI is absolute or * <code>null</null> if the URI is * relative or does not contain a valid host name. * * @since 4.1 */ public static HttpHost extractHost(final URI uri) { if (uri == null) { return null; } HttpHost target = null; if (uri.isAbsolute()) { int port = uri.getPort(); // may be overridden later String host = uri.getHost(); if (host == null) { // normal parse failed; let's do it ourselves // authority does not seem to care about the valid character-set // for host names host = uri.getAuthority(); if (host != null) { // Strip off any leading user credentials int at = host.indexOf('@'); if (at >= 0) { if (host.length() > at + 1) { host = host.substring(at + 1); } else { host = null; // @ on its own } } // Extract the port suffix, if present if (host != null) { int colon = host.indexOf(':'); if (colon >= 0) { int pos = colon + 1; int len = 0; for (int i = pos; i < host.length(); i++) { if (Character.isDigit(host.charAt(i))) { len++; } else { break; } } if (len > 0) { try { port = Integer.parseInt(host.substring(pos, pos + len)); } catch (NumberFormatException ex) { } } host = host.substring(0, colon); } } } } String scheme = uri.getScheme(); if (host != null) { target = new HttpHost(host, port, scheme); } } return target; }
From source file:com.welfare.common.util.UtilValidate.java
/** * Returns true if character c is a digit (0 .. 9). *///from www . j a v a2 s .c o m public static boolean isDigit(char c) { return Character.isDigit(c); }
From source file:com.cyberway.issue.io.arc.ARCReader.java
protected boolean isNumber(final String n) { for (int i = 0; i < n.length(); i++) { if (!Character.isDigit(n.charAt(i))) { return false; }//from w ww . ja v a2 s . c o m } return true; }