Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.bugreport.util;
     18 
     19 import java.util.Calendar;
     20 import java.util.GregorianCalendar;
     21 import java.util.TimeZone;
     22 import java.util.regex.Matcher;
     23 
     24 /**
     25  * Random collection of stuff. Mostly for parsing.
     26  */
     27 public class Utils {
     28     /**
     29      * UTC Time Zone.
     30      */
     31     public static final TimeZone UTC = TimeZone.getTimeZone("UTC");
     32 
     33     /**
     34      * Regex for a date/time, without milliseconds.
     35      */
     36     public static final String DATE_TIME_MS_PATTERN
     37             = "(?:(\\d\\d\\d\\d)-)?(\\d\\d)-(\\d\\d)\\s+(\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d\\d)";
     38 
     39     /**
     40      * Regex for a date/time, without milliseconds.
     41      */
     42     public static final String DATE_TIME_PATTERN
     43             = "(?:(\\d\\d\\d\\d)-)?(\\d\\d)-(\\d\\d)\\s+(\\d\\d):(\\d\\d):(\\d\\d)";
     44 
     45     /**
     46      * Returns whether text matches the matcher.  The matcher can be used afterwards
     47      * to get the groups.
     48      */
     49     public static boolean matches(Matcher matcher, String text) {
     50         matcher.reset(text);
     51         return matcher.matches();
     52     }
     53 
     54     /**
     55      * Returns the matcher if it matches the text, null otherwise.
     56      */
     57     public static Matcher match(Matcher matcher, String text) {
     58         matcher.reset(text);
     59         if (matcher.matches()) {
     60             return matcher;
     61         } else {
     62             return null;
     63         }
     64     }
     65 
     66     /**
     67      * Gets a group from the matcher, and if it was set returns that
     68      * value as an int.  Otherwise returns def.
     69      */
     70     public static int getInt(Matcher matcher, int group, int def) {
     71         final String str = matcher.group(group);
     72         if (str == null || str.length() == 0) {
     73             return def;
     74         } else {
     75             return Integer.parseInt(str);
     76         }
     77     }
     78 
     79     /**
     80      * Gets the date time groups from the matcher and returns a GregorianCalendar.
     81      * The year is optional.
     82      *
     83      * @param Matcher a matcher
     84      * @param startGroup the index of the first group to use
     85      * @param milliseconds whether to expect the millisecond group.
     86      *
     87      * @see #DATE_TIME_MS_PATTERN
     88      * @see #DATE_TIME_PATTERN
     89      */
     90     public static GregorianCalendar parseCalendar(Matcher matcher, int startGroup,
     91             boolean milliseconds) {
     92         final GregorianCalendar result = new GregorianCalendar(UTC);
     93 
     94         if (matcher.group(startGroup+0) != null) {
     95             result.set(Calendar.YEAR, Integer.parseInt(matcher.group(startGroup + 0)));
     96         }
     97         result.set(Calendar.MONTH, Integer.parseInt(matcher.group(startGroup + 1)));
     98         result.set(Calendar.DAY_OF_MONTH, Integer.parseInt(matcher.group(startGroup + 2)));
     99         result.set(Calendar.HOUR_OF_DAY, Integer.parseInt(matcher.group(startGroup + 3)));
    100         result.set(Calendar.MINUTE, Integer.parseInt(matcher.group(startGroup + 4)));
    101         result.set(Calendar.SECOND, Integer.parseInt(matcher.group(startGroup + 5)));
    102         if (milliseconds) {
    103             result.set(Calendar.MILLISECOND, Integer.parseInt(matcher.group(startGroup + 6)));
    104         }
    105 
    106         return result;
    107     }
    108 }
    109