alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.search.types;


import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.util.HelpCtx;


/**
 * Test DataObject primaryFile for modificaion date.
 * 

There are mutually exclusive criteria: between and days. * Between represents absolute date interval. Days represents * relative interval related to today. * *

Internally uses null as wildcard. It is presented as an empty string. * One of between or days must be null. * * @author Petr Kuzel */ public class ModificationDateType extends DataObjectType { private static final long serialVersionUID = 4L; //private static final long serialVersionUID = -5372364935321919998L; /** Holds value of property matchBefore. */ private Date matchBefore; /** Holds value of property matchAfter. */ private Date matchAfter; /** Holds value of property day. */ private Short days; /** * Does the DataObject pass this criterion? * @param dataobject to be examined * @return true if pass */ public boolean testDataObject(DataObject dobj) { FileObject fo = dobj.getPrimaryFile(); // it is strange if(fo == null) return false; // Primary File Modification Date Date date = fo.lastModified(); boolean hit = testDays(date) && testAfter(date) && testBefore(date); if(hit) return true; else return false; } /** Is within range? */ private boolean testAfter(Date date) { if (matchAfter == null) return true; return date.compareTo(matchAfter)>=0; } /** Is within range? */ private boolean testBefore(Date date) { if (matchBefore == null) return true; return date.compareTo(matchBefore)<=0; } /** Is within 24 hours range? */ private boolean testDays(Date date) { if (days == null) return true; return (System.currentTimeMillis() - date.getTime()) < days.shortValue()*1000L*60L*60L*24L; } // -------------- before ------------------- /** Getter for property matchBefore. *@return Value of property matchBefore. */ public Date getMatchBeforeAsDate() { return new FormattedDate(matchBefore); } /** Getter of matchBefore property. */ public String getMatchBefore() { if (matchBefore == null) return ""; // NOI18N else return getMatchBeforeAsDate().toString(); } /** Setter for property matchBefore. * @exception IllegalArgumentException if null passed */ public void setMatchBefore(String before) { try { setMatchBeforeImpl(before); setValid (this.matchBefore != null); } catch (IllegalArgumentException ex) { setValid(false); throw ex; } } /** Helper method for setting matchBefore property. */ private void setMatchBeforeImpl(String matchBefore) { if(matchBefore == null) throw new IllegalArgumentException(); if(matchBefore.equals("")) { // NOI18N setMatchBeforeByDate(null); return; } try { setMatchBeforeByDate(new FormattedDate(matchBefore)); } catch (ParseException ex) { throw new IllegalArgumentException(); } } /** Sets property matchBefore according specified date. * @param matchBefore new value of property matchBefore */ public void setMatchBeforeByDate(Date matchBefore) { //let date represent one milisecond before midnight if (matchBefore != null) { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(matchBefore); cal.set(cal.HOUR_OF_DAY, cal.getActualMaximum(cal.HOUR_OF_DAY)); cal.set(cal.MINUTE, cal.getActualMaximum(cal.MINUTE)); cal.set(cal.SECOND, cal.getActualMaximum(cal.SECOND)); cal.set(cal.MILLISECOND, cal.getActualMaximum(cal.MILLISECOND)); matchBefore = cal.getTime(); } //do it this.matchBefore = matchBefore; days = null; } // --------------- after --------------- /** Gets for property matchAfter according specified date. * @return value of property matchAfter */ public Date getMatchAfterAsDate() { return new FormattedDate(matchAfter); } /** Getter for property matchAfter. */ public String getMatchAfter() { if (matchAfter == null) return ""; // NOI18N else return getMatchAfterAsDate().toString(); } /** Setter for property matchAfter. * @exception IllegalArgumentException if null passed */ public void setMatchAfter(String after) { try { setMatchAfterImpl(after); setValid (this.matchAfter != null); } catch (IllegalArgumentException ex) { setValid(false); throw ex; } } /** Helper method which sets matchAfter property. */ private void setMatchAfterImpl(String matchAfter) { if (matchAfter == null) throw new IllegalArgumentException(); if (matchAfter.equals("")) { // NOI18N setMatchAfterByDate(null); return; } try { setMatchAfterByDate(new FormattedDate(matchAfter)); } catch (ParseException ex) { throw new IllegalArgumentException(); } } /** Setter for property matchAfter. *@param matchAfter New value of property matchAfter. */ public void setMatchAfterByDate(Date matchAfter) { //let date represent midnight if (matchAfter != null) { GregorianCalendar cal = new GregorianCalendar(); cal.setTime(matchAfter); cal.set(cal.HOUR_OF_DAY, cal.getActualMinimum(cal.HOUR_OF_DAY)); cal.set(cal.MINUTE, cal.getActualMinimum(cal.MINUTE)); cal.set(cal.SECOND, cal.getActualMinimum(cal.SECOND)); cal.set(cal.MILLISECOND, cal.getActualMinimum(cal.MILLISECOND)); matchAfter = cal.getTime(); } //do it this.matchAfter = matchAfter; days = null; } // ------------ days handling ---------------------- /** Gets property days ad short. * @return value of property day */ public Short getDaysAsShort() { return days; } /** Getter for property days. */ public String getDays() { if(days == null) { return ""; // NOI18N } else { return days.toString(); } } /** Setter for property days. * @param String to be parsed as short */ public void setDays(String days) { try { setDaysImpl(days); setValid (this.days != null); } catch (IllegalArgumentException ex) { setValid(false); throw ex; } } /** Sets days property. Helper method. */ private void setDaysImpl(String days) { if("".equals(days)) { // NOI18N setDaysByShort(null); return; } try { DecimalFormat format = new DecimalFormat(); setDaysByShort(new Short(format.parse(days).shortValue())); } catch (ParseException ex) { throw new IllegalArgumentException(); } } /** Setter for property day. * @param day new value of property day */ private void setDaysByShort(Short days) { this.days = days; matchAfter = null; matchBefore = null; } /** Gets help context for this search type. * Implements superclass abstract method. */ public HelpCtx getHelpCtx() { return new HelpCtx(ModificationDateType.class); } /** * Date using default locale formatting. * Provide overridden constructor and toString() method. */ static class FormattedDate extends Date { /** Default locale formattor. */ private static DateFormat format; /** isNull property */ private transient boolean isNull = true; /** Init static fields */ static { format = new SimpleDateFormat().getDateInstance(); } /** Create new object. * @param date null is ambiguous -> today. */ public FormattedDate(Date date) { super(date == null ? new Date().getTime() : date.getTime() ); isNull = date == null; } /** Create new object. * @param Use default locale formatting while parsing date */ public FormattedDate(String date) throws ParseException { super( format.parse(date).getTime() ); isNull = date == null; } /** Use defalt locale formatting. */ public String toString() { return format.format(this); } /** * Extra handling of equals(null). Return true if * this Date represents empty string value. */ public boolean equals(Object obj){ if (obj == null && isNull) return true; else return super.equals(obj); } } }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.