Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. import java.util.Calendar;
  2.  
  3. /**
  4.  * Store the data from a single line of a
  5.  * web-server log file.
  6.  * Individual fields are made available via
  7.  * accessors such as getHour() and getMinute().
  8.  *
  9.  * @author David J. Barnes and Michael Kölling.
  10.  * @version    2016.02.29
  11.  * Exercise 7.19
  12.  */
  13. public class LogEntry implements Comparable<LogEntry>
  14. {
  15.     // Where the data values extracted from a single
  16.     // log line are stored.
  17.     private int[] dataValues;
  18.     // The equivalent Calendar object for the log time.
  19.     private Calendar when;
  20.    
  21.     // At which index in dataValues the different fields
  22.     // from a log line are stored.
  23.     private static final int YEAR = 0, MONTH = 1, DAY = 2,
  24.                              HOUR = 3, MINUTE = 4;
  25.     // The number of fields. If more fields are added, e.g. for
  26.     // seconds or a status code, then this value must be increased
  27.     // to match.
  28.     private static final int NUMBER_OF_FIELDS = 5;
  29.                      
  30.     /**
  31.      * Decompose a log line so that the individual fields
  32.      * are available.
  33.      * @param logline A single line from the log.
  34.      *                This should be in the format:
  35.      *                year month day hour minute etc.
  36.      */
  37.     public LogEntry(String logline)
  38.     {
  39.         // The array to store the data for a single line.
  40.         dataValues = new int[NUMBER_OF_FIELDS];
  41.         // Break up the log line.
  42.         LoglineTokenizer tokenizer = new LoglineTokenizer();
  43.         tokenizer.tokenize(logline,dataValues);
  44.         setWhen();
  45.     }
  46.    
  47.     /**
  48.      * Create a LogEntry from the individual components.
  49.      * @param year The year
  50.      * @param month The month (1-12)
  51.      * @param day The day (1-31)
  52.      * @param hour The hour (0-23)
  53.      * @param minute The minute (0-59)
  54.      */
  55.     public LogEntry(int year, int month, int day, int hour, int minute)
  56.     {
  57.         // The array to store the data for a single line.
  58.         dataValues = new int[NUMBER_OF_FIELDS];
  59.         dataValues[YEAR] = year;
  60.         dataValues[MONTH] = month;
  61.         dataValues[DAY] = day;
  62.         dataValues[HOUR] = hour;
  63.         dataValues[MINUTE] = minute;
  64.         setWhen();
  65.     }
  66.    
  67.     /**
  68.      * Return the hour.
  69.      * @return The hour field from the log line.
  70.      */
  71.     public int getHour()
  72.     {
  73.         return dataValues[HOUR];
  74.     }
  75.  
  76.     /**
  77.      * Return the minute.
  78.      * @return The minute field from the log line.
  79.      */
  80.     public int getMinute()
  81.     {
  82.         return dataValues[MINUTE];
  83.     }
  84.    
  85.     /**
  86.      * Return the year.
  87.      * @return The year field from the log line.
  88.      */
  89.     public int getYear()
  90.     {
  91.         return dataValues[YEAR];
  92.     }
  93.    
  94.     /**
  95.      * Return the month.
  96.      * @return The month field from the log line.
  97.      */
  98.     public int getMonth()
  99.     {
  100.         return dataValues[MONTH];
  101.     }
  102.    
  103.     /**
  104.      * Return the day.
  105.      * @return The day field from the log line.
  106.      */
  107.     public int getDay()
  108.     {
  109.         return dataValues[DAY];
  110.     }
  111.    
  112.     /**
  113.      * Create a string representation of the data.
  114.      * This is not necessarily identical with the
  115.      * text of the original log line.
  116.      * @return A string representing the data of this entry.
  117.      */
  118.     public String toString()
  119.     {
  120.         StringBuffer buffer = new StringBuffer();
  121.         for(int value : dataValues) {
  122.            // Prefix a leading zero on single digit numbers.
  123.             if(value < 10) {
  124.                 buffer.append('0');
  125.             }
  126.             buffer.append(value);
  127.             buffer.append(' ');
  128.         }
  129.         // Drop any trailing space.
  130.         return buffer.toString().trim();
  131.     }
  132.    
  133.     /**
  134.      * Compare the date/time combination of this log entry
  135.      * with another.
  136.      * @param otherEntry The other entry to compare against.
  137.      * @return A negative value if this entry comes before the other.
  138.      *         A positive value if this entry comes after the other.
  139.      *         Zero if the entries are the same.
  140.      */
  141.     public int compareTo(LogEntry otherEntry)
  142.     {
  143.         // Use the equivalent Calendars comparison method.
  144.         return when.compareTo(otherEntry.getWhen());
  145.     }
  146.    
  147.     /**
  148.      * Return the Calendar object representing this event.
  149.      * @return The Calendar for this event.
  150.      */
  151.     private Calendar getWhen()
  152.     {
  153.         return when;
  154.     }
  155.  
  156.     /**
  157.      * Create an equivalent Calendar object from the data values.
  158.      */
  159.     private void setWhen()
  160.     {
  161.         when = Calendar.getInstance();
  162.         // Adjust from 1-based month and day to 0-based.
  163.         when.set(dataValues[YEAR],
  164.                  dataValues[MONTH] - 1, dataValues[DAY] - 1,
  165.                  dataValues[HOUR], dataValues[MINUTE]);
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement