+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-public class MConstraintFilteringException extends MFilteringException {
-
- private static final long serialVersionUID = 0L;
-
- /* */
-
- public MConstraintFilteringException() {
- super();
- }
-
- public MConstraintFilteringException(String message) {
- super(message);
- }
-
- public MConstraintFilteringException(Throwable error) {
- super(error);
- }
-
- public MConstraintFilteringException(String message, Throwable error) {
- super(message, error);
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.Locale;
-import java.util.TimeZone;
-
-public class MDateFilter extends MFilter {
-
- protected LinkedHashSet<String> userDateFormats = new LinkedHashSet<String>();
- protected Locale userLocale = null;
- protected TimeZone userTimeZone = null;
- protected String storageDateFormat = null;
- protected Locale storageLocale = null;
- protected TimeZone storageTimeZone = null;
-
- /* */
-
- public MDateFilter(String defaultUserDateFormat, Locale userLocale, String storageDateFormat, Locale storageLocale) {
- this(defaultUserDateFormat, userLocale, TimeZone.getTimeZone("UTC"), storageDateFormat, storageLocale, TimeZone.getTimeZone("UTC"));
- }
-
- public MDateFilter(String defaultUserDateFormat, Locale userLocale, TimeZone userTimeZone, String storageDateFormat, Locale storageLocale, TimeZone storageTimeZone) {
- super();
- //
- this.addUserDateFormat(defaultUserDateFormat);
- this.setUserLocale(userLocale);
- this.setUserTimeZone(userTimeZone);
- this.setStorageDateFormat(storageDateFormat);
- this.setStorageLocale(storageLocale);
- this.setStorageTimeZone(storageTimeZone);
- }
-
- public MDateFilter(LinkedHashSet<String> userDateFormats, Locale userLocale, TimeZone userTimeZone, String storageDateFormat, Locale storageLocale, TimeZone storageTimeZone) {
- super();
- //
- this.setUserDateFormats(userDateFormats);
- this.setUserLocale(userLocale);
- this.setUserTimeZone(userTimeZone);
- this.setStorageDateFormat(storageDateFormat);
- this.setStorageLocale(storageLocale);
- this.setStorageTimeZone(storageTimeZone);
- }
-
- public MDateFilter clone() {
- return new MDateFilter(this.getUserDateFormats(), this.getUserLocale(), this.getUserTimeZone(), this.getStorageDateFormat(), this.getStorageLocale(), this.getStorageTimeZone());
- }
-
- /* User date formats */
-
- public void setUserDateFormats(LinkedHashSet<String> userDateFormats) {
- if (null == userDateFormats) {
- throw new IllegalArgumentException("Invalid 'userDateFormats': null.");
- }
- else {
- Iterator i = userDateFormats.iterator();
- while (i.hasNext()) {
- MDateFilter.checkDateFormat((String)i.next());
- }
- }
- //
- synchronized (this.userDateFormats) {
- this.userDateFormats = userDateFormats;
- }
- }
-
- public void addUserDateFormat(String userDateFormat) {
- MDateFilter.checkDateFormat(userDateFormat);
- //
- synchronized (this.userDateFormats) {
- this.userDateFormats.add(userDateFormat);
- }
- }
-
- protected LinkedHashSet<String> getUserDateFormatsReference() {
- return this.userDateFormats;
- }
-
- public LinkedHashSet<String> getUserDateFormats() {
- LinkedHashSet<String> tmpUserDateFormats = new LinkedHashSet<String>();
- Iterator i = this.getUserDateFormatsReference().iterator();
- while (i.hasNext()) {
- tmpUserDateFormats.add((String)i.next());
- }
- return tmpUserDateFormats;
- }
-
- public String getDefaultUserDateFormat() {
- return this.getUserDateFormatsReference().iterator().next();
- }
-
- /* User locale */
-
- protected void setUserLocale(Locale userLocale) {
- if (null == userLocale) {
- throw new IllegalArgumentException("Invalid 'userLocale': null.");
- }
- //
- this.userLocale = userLocale;
- }
-
- protected Locale getUserLocaleReference() {
- return this.userLocale;
- }
-
- public Locale getUserLocale() {
- return (Locale)this.getUserLocaleReference().clone();
- }
-
- /* User time zone */
-
- protected void setUserTimeZone(TimeZone userTimeZone) {
- if (null == userTimeZone) {
- throw new IllegalArgumentException("Invalid 'userTimeZone': null.");
- }
- //
- this.userTimeZone = userTimeZone;
- }
-
- protected TimeZone getUserTimeZoneReference() {
- return this.userTimeZone;
- }
-
- public TimeZone getUserTimeZone() {
- return (TimeZone)this.getUserTimeZoneReference().clone();
- }
-
- /* Storage date format */
-
- protected void setStorageDateFormat(String storageDateFormat) {
- MDateFilter.checkDateFormat(storageDateFormat);
- //
- this.storageDateFormat = storageDateFormat;
- }
-
- public String getStorageDateFormat() {
- return this.storageDateFormat;
- }
-
- /* Storage locale */
-
- protected void setStorageLocale(Locale storageLocale) {
- if (null == storageLocale) {
- throw new IllegalArgumentException("Invalid 'storageLocale': null.");
- }
- //
- this.storageLocale = storageLocale;
- }
-
- protected Locale getStorageLocaleReference() {
- return this.storageLocale;
- }
-
- public Locale getStorageLocale() {
- return (Locale)this.getStorageLocaleReference().clone();
- }
-
- /* Storage time zone */
-
- protected void setStorageTimeZone(TimeZone storageTimeZone) {
- if (null == storageTimeZone) {
- throw new IllegalArgumentException("Invalid 'storageTimeZone': null.");
- }
- //
- this.storageTimeZone = storageTimeZone;
- }
-
- protected TimeZone getStorageTimeZoneReference() {
- return this.storageTimeZone;
- }
-
- public TimeZone getStorageTimeZone() {
- return (TimeZone)this.getStorageTimeZoneReference().clone();
- }
-
- /* Date-specific methods */
-
- protected static void checkDateFormat(String dateFormat) {
- if ((null == dateFormat) || ("".equals(dateFormat))) {
- throw new IllegalArgumentException("Invalid 'dateFormat': null or empty.");
- }
- //
- try {
- SimpleDateFormat testDateFormat = new SimpleDateFormat(dateFormat);
- }
- catch (IllegalArgumentException exception) {
- throw new IllegalArgumentException(String.format("Invalid 'dateFormat': %s.", dateFormat)); // no need to propagate exception
- }
- }
-
- protected static LinkedHashMap<String, String> getDateConstraintMap(String constraints) {
- LinkedHashSet<String> validConstraints = new LinkedHashSet<String>();
- validConstraints.add("notNull");
- validConstraints.add("minimumValue");
- validConstraints.add("maximumValue");
- return MDateFilter.getConstraintMap(constraints, validConstraints);
- }
-
- protected static Date getDateObject(String x, String inputDateFormat, Locale inputLocale, TimeZone inputTimeZone) throws MFormatFilteringException {
- if ((null == x) || ("".equals(x))) {
- throw new IllegalArgumentException("Invalid 'x': null or empty.");
- }
- MDateFilter.checkDateFormat(inputDateFormat);
- if (null == inputLocale) {
- throw new IllegalArgumentException("Invalid 'inputLocale': null.");
- }
- if (null == inputTimeZone) {
- throw new IllegalArgumentException("Invalid 'inputTimeZone': null.");
- }
- //
- Calendar c1 = Calendar.getInstance(inputTimeZone, inputLocale);
- c1.clear();
- c1.setLenient(false);
- SimpleDateFormat sdf = new SimpleDateFormat(inputDateFormat, inputLocale);
- sdf.setCalendar(c1);
- Date d1 = null;
- try {
- d1 = sdf.parse(x);
- }
- catch (ParseException exception) {
- throw new MFormatFilteringException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDateFormat)); // no need to propagate exception
- }
- Calendar c2 = Calendar.getInstance(inputTimeZone, inputLocale);
- c2.clear();
- c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
- c2.set(Calendar.MONTH, c1.get(Calendar.MONTH));
- c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
- Date d2 = c2.getTime();
- if (!x.equals(sdf.format(d2))) {
- throw new MFormatFilteringException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDateFormat));
- }
- return d2;
- }
-
- protected static String getDateString(Date date, String outputDateFormat, Locale outputLocale, TimeZone outputTimeZone) {
- if (null == date) {
- throw new IllegalArgumentException("Invalid 'date': null.");
- }
- MDateFilter.checkDateFormat(outputDateFormat);
- if (null == outputLocale) {
- throw new IllegalArgumentException("Invalid 'outputLocale': null.");
- }
- if (null == outputTimeZone) {
- throw new IllegalArgumentException("Invalid 'outputTimeZone': null.");
- }
- //
- SimpleDateFormat sdf = new SimpleDateFormat(outputDateFormat, outputLocale);
- sdf.setCalendar(Calendar.getInstance(outputTimeZone, outputLocale));
- return sdf.format(date);
- }
-
- protected String getRebuiltDateString(String x, String inputDateFormat, Locale inputLocale, TimeZone inputTimeZone, String outputDateFormat, Locale outputLocale, TimeZone outputTimeZone, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- LinkedHashMap<String, String> constraintMap = MDateFilter.getDateConstraintMap(constraints);
- if ((null == x) || ("".equals(x))) {
- if ("true".equals(constraintMap.get("notNull"))) {
- throw new MConstraintFilteringException("Invalid 'x': cannot be null or empty.");
- }
- return "";
- }
- Date date = MDateFilter.getDateObject(x, inputDateFormat, inputLocale, inputTimeZone);
- if (constraintMap.containsKey("minimumValue")) {
- Date constraintDate = MDateFilter.getDateObject(constraintMap.get("minimumValue"), this.getStorageDateFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- if (date.compareTo(constraintDate) < 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be >= %s.", x, constraintMap.get("minimumValue")));
- }
- }
- if (constraintMap.containsKey("maximumValue")) {
- Date constraintDate = MDateFilter.getDateObject(constraintMap.get("maximumValue"), this.getStorageDateFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- if (date.compareTo(constraintDate) > 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be <= %s.", x, constraintMap.get("maximumValue")));
- }
- }
- return MDateFilter.getDateString(date, outputDateFormat, outputLocale, outputTimeZone);
- }
-
- public String getValidatedUserDate(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userDateFormat: this.getUserDateFormatsReference()) {
- try {
- y = this.getRebuiltDateString(x, userDateFormat, this.getUserLocaleReference(), this.getUserTimeZoneReference(), this.getDefaultUserDateFormat(), this.getUserLocaleReference(), this.getUserTimeZoneReference(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getStorageDateFromUserDate(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userDateFormat: this.getUserDateFormatsReference()) {
- try {
- y = this.getRebuiltDateString(x, userDateFormat, this.getUserLocaleReference(), this.getUserTimeZoneReference(), this.getStorageDateFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getValidatedStorageDate(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltDateString(x, this.getStorageDateFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), this.getStorageDateFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), constraints);
- }
-
- public String getUserDateFromStorageDate(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltDateString(x, this.getStorageDateFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), this.getDefaultUserDateFormat(), this.getUserLocaleReference(), this.getUserTimeZoneReference(), constraints);
- }
-
- public Date getDateObjectFromStorageDate(String x) throws MFormatFilteringException {
- return MDateFilter.getDateObject(x, this.getStorageDateFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.Locale;
-import java.util.TimeZone;
-
-public class MDatetimeFilter extends MFilter {
-
- protected LinkedHashSet<String> userDatetimeFormats = new LinkedHashSet<String>();
- protected Locale userLocale = null;
- protected TimeZone userTimeZone = null;
- protected String storageDatetimeFormat = null;
- protected Locale storageLocale = null;
- protected TimeZone storageTimeZone = null;
-
- /* */
-
- public MDatetimeFilter(String defaultUserDatetimeFormat, Locale userLocale, String storageDatetimeFormat, Locale storageLocale) {
- this(defaultUserDatetimeFormat, userLocale, TimeZone.getTimeZone("UTC"), storageDatetimeFormat, storageLocale, TimeZone.getTimeZone("UTC"));
- }
-
- public MDatetimeFilter(String defaultUserDatetimeFormat, Locale userLocale, TimeZone userTimeZone, String storageDatetimeFormat, Locale storageLocale, TimeZone storageTimeZone) {
- super();
- //
- this.addUserDatetimeFormat(defaultUserDatetimeFormat);
- this.setUserLocale(userLocale);
- this.setUserTimeZone(userTimeZone);
- this.setStorageDatetimeFormat(storageDatetimeFormat);
- this.setStorageLocale(storageLocale);
- this.setStorageTimeZone(storageTimeZone);
- }
-
- public MDatetimeFilter(LinkedHashSet<String> userDatetimeFormats, Locale userLocale, TimeZone userTimeZone, String storageDatetimeFormat, Locale storageLocale, TimeZone storageTimeZone) {
- super();
- //
- this.setUserDatetimeFormats(userDatetimeFormats);
- this.setUserLocale(userLocale);
- this.setUserTimeZone(userTimeZone);
- this.setStorageDatetimeFormat(storageDatetimeFormat);
- this.setStorageLocale(storageLocale);
- this.setStorageTimeZone(storageTimeZone);
- }
-
- public MDatetimeFilter clone() {
- return new MDatetimeFilter(this.getUserDatetimeFormats(), this.getUserLocale(), this.getUserTimeZone(), this.getStorageDatetimeFormat(), this.getStorageLocale(), this.getStorageTimeZone());
- }
-
- /* User datetime formats */
-
- public void setUserDatetimeFormats(LinkedHashSet<String> userDatetimeFormats) {
- if (null == userDatetimeFormats) {
- throw new IllegalArgumentException("Invalid 'userDatetimeFormats': null.");
- }
- else {
- Iterator i = userDatetimeFormats.iterator();
- while (i.hasNext()) {
- MDatetimeFilter.checkDatetimeFormat((String)i.next());
- }
- }
- //
- synchronized (this.userDatetimeFormats) {
- this.userDatetimeFormats = userDatetimeFormats;
- }
- }
-
- public void addUserDatetimeFormat(String userDatetimeFormat) {
- MDatetimeFilter.checkDatetimeFormat(userDatetimeFormat);
- //
- synchronized (this.userDatetimeFormats) {
- this.userDatetimeFormats.add(userDatetimeFormat);
- }
- }
-
- protected LinkedHashSet<String> getUserDatetimeFormatsReference() {
- return this.userDatetimeFormats;
- }
-
- public LinkedHashSet<String> getUserDatetimeFormats() {
- LinkedHashSet<String> tmpUserDatetimeFormats = new LinkedHashSet<String>();
- Iterator i = this.getUserDatetimeFormatsReference().iterator();
- while (i.hasNext()) {
- tmpUserDatetimeFormats.add((String)i.next());
- }
- return tmpUserDatetimeFormats;
- }
-
- public String getDefaultUserDatetimeFormat() {
- return this.getUserDatetimeFormatsReference().iterator().next();
- }
-
- /* User locale */
-
- protected void setUserLocale(Locale userLocale) {
- if (null == userLocale) {
- throw new IllegalArgumentException("Invalid 'userLocale': null.");
- }
- //
- this.userLocale = userLocale;
- }
-
- protected Locale getUserLocaleReference() {
- return this.userLocale;
- }
-
- public Locale getUserLocale() {
- return (Locale)this.getUserLocaleReference().clone();
- }
-
- /* User time zone */
-
- protected void setUserTimeZone(TimeZone userTimeZone) {
- if (null == userTimeZone) {
- throw new IllegalArgumentException("Invalid 'userTimeZone': null.");
- }
- //
- this.userTimeZone = userTimeZone;
- }
-
- protected TimeZone getUserTimeZoneReference() {
- return this.userTimeZone;
- }
-
- public TimeZone getUserTimeZone() {
- return (TimeZone)this.getUserTimeZoneReference().clone();
- }
-
- /* Storage datetime format */
-
- protected void setStorageDatetimeFormat(String storageDatetimeFormat) {
- MDatetimeFilter.checkDatetimeFormat(storageDatetimeFormat);
- //
- this.storageDatetimeFormat = storageDatetimeFormat;
- }
-
- public String getStorageDatetimeFormat() {
- return this.storageDatetimeFormat;
- }
-
- /* Storage locale */
-
- protected void setStorageLocale(Locale storageLocale) {
- if (null == storageLocale) {
- throw new IllegalArgumentException("Invalid 'storageLocale': null.");
- }
- //
- this.storageLocale = storageLocale;
- }
-
- protected Locale getStorageLocaleReference() {
- return this.storageLocale;
- }
-
- public Locale getStorageLocale() {
- return (Locale)this.getStorageLocaleReference().clone();
- }
-
- /* Storage time zone */
-
- protected void setStorageTimeZone(TimeZone storageTimeZone) {
- if (null == storageTimeZone) {
- throw new IllegalArgumentException("Invalid 'storageTimeZone': null.");
- }
- //
- this.storageTimeZone = storageTimeZone;
- }
-
- protected TimeZone getStorageTimeZoneReference() {
- return this.storageTimeZone;
- }
-
- public TimeZone getStorageTimeZone() {
- return (TimeZone)this.getStorageTimeZoneReference().clone();
- }
-
- /* Datetime-specific methods */
-
- protected static void checkDatetimeFormat(String datetimeFormat) {
- if ((null == datetimeFormat) || ("".equals(datetimeFormat))) {
- throw new IllegalArgumentException("Invalid 'datetimeFormat': null or empty.");
- }
- //
- try {
- SimpleDateFormat testDatetimeFormat = new SimpleDateFormat(datetimeFormat);
- }
- catch (IllegalArgumentException exception) {
- throw new IllegalArgumentException(String.format("Invalid 'datetimeFormat': %s.", datetimeFormat)); // no need to propagate exception
- }
- }
-
- protected static LinkedHashMap<String, String> getDatetimeConstraintMap(String constraints) {
- LinkedHashSet<String> validConstraints = new LinkedHashSet<String>();
- validConstraints.add("notNull");
- validConstraints.add("minimumValue");
- validConstraints.add("maximumValue");
- return MDatetimeFilter.getConstraintMap(constraints, validConstraints);
- }
-
- protected static Date getDatetimeObject(String x, String inputDatetimeFormat, Locale inputLocale, TimeZone inputTimeZone) throws MFormatFilteringException {
- if ((null == x) || ("".equals(x))) {
- throw new IllegalArgumentException("Invalid 'x': null or empty.");
- }
- MDatetimeFilter.checkDatetimeFormat(inputDatetimeFormat);
- if (null == inputLocale) {
- throw new IllegalArgumentException("Invalid 'inputLocale': null.");
- }
- if (null == inputTimeZone) {
- throw new IllegalArgumentException("Invalid 'inputTimeZone': null.");
- }
- //
- Calendar c1 = Calendar.getInstance(inputTimeZone, inputLocale);
- c1.clear();
- c1.setLenient(false);
- SimpleDateFormat sdf = new SimpleDateFormat(inputDatetimeFormat, inputLocale);
- sdf.setCalendar(c1);
- Date d1 = null;
- try {
- d1 = sdf.parse(x);
- }
- catch (ParseException exception) {
- throw new MFormatFilteringException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDatetimeFormat)); // no need to propagate exception
- }
- Calendar c2 = Calendar.getInstance(inputTimeZone, inputLocale);
- c2.clear();
- c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
- c2.set(Calendar.MONTH, c1.get(Calendar.MONTH));
- c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
- c2.set(Calendar.HOUR_OF_DAY, c1.get(Calendar.HOUR_OF_DAY));
- c2.set(Calendar.MINUTE, c1.get(Calendar.MINUTE));
- c2.set(Calendar.SECOND, c1.get(Calendar.SECOND));
- Date d2 = c2.getTime();
- if (!x.equals(sdf.format(d2))) {
- throw new MFormatFilteringException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDatetimeFormat));
- }
- return d2;
- }
-
- protected static String getDatetimeString(Date datetime, String outputDatetimeFormat, Locale outputLocale, TimeZone outputTimeZone) {
- if (null == datetime) {
- throw new IllegalArgumentException("Invalid 'datetime': null.");
- }
- MDatetimeFilter.checkDatetimeFormat(outputDatetimeFormat);
- if (null == outputLocale) {
- throw new IllegalArgumentException("Invalid 'outputLocale': null.");
- }
- if (null == outputTimeZone) {
- throw new IllegalArgumentException("Invalid 'outputTimeZone': null.");
- }
- //
- SimpleDateFormat sdf = new SimpleDateFormat(outputDatetimeFormat, outputLocale);
- sdf.setCalendar(Calendar.getInstance(outputTimeZone, outputLocale));
- return sdf.format(datetime);
- }
-
- protected String getRebuiltDatetimeString(String x, String inputDatetimeFormat, Locale inputLocale, TimeZone inputTimeZone, String outputDatetimeFormat, Locale outputLocale, TimeZone outputTimeZone, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- LinkedHashMap<String, String> constraintMap = MDatetimeFilter.getDatetimeConstraintMap(constraints);
- if ((null == x) || ("".equals(x))) {
- if ("true".equals(constraintMap.get("notNull"))) {
- throw new MConstraintFilteringException("Invalid 'x': cannot be null or empty.");
- }
- return "";
- }
- Date datetime = MDatetimeFilter.getDatetimeObject(x, inputDatetimeFormat, inputLocale, inputTimeZone);
- if (constraintMap.containsKey("minimumValue")) {
- Date constraintDatetime = MDatetimeFilter.getDatetimeObject(constraintMap.get("minimumValue"), this.getStorageDatetimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- if (datetime.compareTo(constraintDatetime) < 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be >= %s.", x, constraintMap.get("minimumValue")));
- }
- }
- if (constraintMap.containsKey("maximumValue")) {
- Date constraintDatetime = MDatetimeFilter.getDatetimeObject(constraintMap.get("maximumValue"), this.getStorageDatetimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- if (datetime.compareTo(constraintDatetime) > 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be <= %s.", x, constraintMap.get("maximumValue")));
- }
- }
- return MDatetimeFilter.getDatetimeString(datetime, outputDatetimeFormat, outputLocale, outputTimeZone);
- }
-
- public String getValidatedUserDatetime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userDatetimeFormat: this.getUserDatetimeFormatsReference()) {
- try {
- y = this.getRebuiltDatetimeString(x, userDatetimeFormat, this.getUserLocaleReference(), this.getUserTimeZoneReference(), this.getDefaultUserDatetimeFormat(), this.getUserLocaleReference(), this.getUserTimeZoneReference(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getStorageDatetimeFromUserDatetime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userDatetimeFormat: this.getUserDatetimeFormatsReference()) {
- try {
- y = this.getRebuiltDatetimeString(x, userDatetimeFormat, this.getUserLocaleReference(), this.getUserTimeZoneReference(), this.getStorageDatetimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getValidatedStorageDatetime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltDatetimeString(x, this.getStorageDatetimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), this.getStorageDatetimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), constraints);
- }
-
- public String getUserDatetimeFromStorageDatetime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltDatetimeString(x, this.getStorageDatetimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), this.getDefaultUserDatetimeFormat(), this.getUserLocaleReference(), this.getUserTimeZoneReference(), constraints);
- }
-
- public Date getDatetimeObjectFromStorageDatetime(String x) throws MFormatFilteringException {
- return MDatetimeFilter.getDatetimeObject(x, this.getStorageDatetimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import com.marcozanon.macaco.MObject;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-
-public abstract class MFilter extends MObject {
-
- /* Helpers */
-
- protected static LinkedHashMap<String, String> getConstraintMap(String constraints, LinkedHashSet<String> validConstraints) {
- if ("".equals(constraints)) {
- throw new IllegalArgumentException("Invalid 'constraints': empty.");
- }
- //
- LinkedHashMap<String, String> constraintMap = new LinkedHashMap<String, String>();
- if (null == constraints) {
- return constraintMap;
- }
- for (String constraint: constraints.split("&")) {
- if (-1 == constraint.indexOf("=")) {
- throw new IllegalArgumentException(String.format("Invalid constraint: %s.", constraint));
- }
- String[] kv = constraint.split("=", 2);
- if ((null != validConstraints) && (!validConstraints.contains(kv[0]))) {
- throw new IllegalArgumentException(String.format("Invalid constraint: %s.", kv[0]));
- }
- constraintMap.put(kv[0], kv[1]);
- }
- return constraintMap;
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import com.marcozanon.macaco.MException;
-
-public abstract class MFilteringException extends MException {
-
- private static final long serialVersionUID = 0L;
-
- /* */
-
- public MFilteringException() {
- super();
- }
-
- public MFilteringException(String message) {
- super(message);
- }
-
- public MFilteringException(Throwable error) {
- super(error);
- }
-
- public MFilteringException(String message, Throwable error) {
- super(message, error);
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-public class MFormatFilteringException extends MFilteringException {
-
- private static final long serialVersionUID = 0L;
-
- /* */
-
- public MFormatFilteringException() {
- super();
- }
-
- public MFormatFilteringException(String message) {
- super(message);
- }
-
- public MFormatFilteringException(Throwable error) {
- super(error);
- }
-
- public MFormatFilteringException(String message, Throwable error) {
- super(message, error);
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import java.math.BigDecimal;
-import java.text.DecimalFormat;
-import java.text.DecimalFormatSymbols;
-import java.text.NumberFormat;
-import java.text.ParsePosition;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.Locale;
-
-public class MNumberFilter extends MFilter {
-
- protected LinkedHashSet<String> userNumberFormats = new LinkedHashSet<String>();
- protected Locale userLocale = null;
- protected char userGroupingSeparator = 0;
- protected char userDecimalSeparator = 0;
- protected String storageNumberFormat = null;
- protected Locale storageLocale = null;
- protected char storageGroupingSeparator = 0;
- protected char storageDecimalSeparator = 0;
-
- /* */
-
- public MNumberFilter(String defaultUserNumberFormat, Locale userLocale, String storageNumberFormat, Locale storageLocale) {
- super();
- //
- this.addUserNumberFormat(defaultUserNumberFormat);
- this.setUserLocale(userLocale);
- this.setStorageNumberFormat(storageNumberFormat);
- this.setStorageLocale(storageLocale);
- }
-
- public MNumberFilter(LinkedHashSet<String> userNumberFormats, Locale userLocale, String storageNumberFormat, Locale storageLocale) {
- super();
- //
- this.setUserNumberFormats(userNumberFormats);
- this.setUserLocale(userLocale);
- this.setStorageNumberFormat(storageNumberFormat);
- this.setStorageLocale(storageLocale);
- }
-
- public MNumberFilter clone() {
- return new MNumberFilter(this.getUserNumberFormats(), this.getUserLocale(), this.getStorageNumberFormat(), this.getStorageLocale());
- }
-
- /* User number formats */
-
- public void setUserNumberFormats(LinkedHashSet<String> userNumberFormats) {
- if (null == userNumberFormats) {
- throw new IllegalArgumentException("Invalid 'userNumberFormats': null.");
- }
- else {
- Iterator i = userNumberFormats.iterator();
- while (i.hasNext()) {
- MNumberFilter.checkNumberFormat((String)i.next());
- }
- }
- //
- synchronized (this.userNumberFormats) {
- this.userNumberFormats = userNumberFormats;
- }
- }
-
- public void addUserNumberFormat(String userNumberFormat) {
- MNumberFilter.checkNumberFormat(userNumberFormat);
- //
- synchronized (this.userNumberFormats) {
- this.userNumberFormats.add(userNumberFormat);
- }
- }
-
- protected LinkedHashSet<String> getUserNumberFormatsReference() {
- return this.userNumberFormats;
- }
-
- public LinkedHashSet<String> getUserNumberFormats() {
- LinkedHashSet<String> tmpUserNumberFormats = new LinkedHashSet<String>();
- Iterator i = this.getUserNumberFormatsReference().iterator();
- while (i.hasNext()) {
- tmpUserNumberFormats.add((String)i.next());
- }
- return tmpUserNumberFormats;
- }
-
- public String getDefaultUserNumberFormat() {
- return this.getUserNumberFormatsReference().iterator().next();
- }
-
- /* User locale */
-
- protected void setUserLocale(Locale userLocale) {
- if (null == userLocale) {
- throw new IllegalArgumentException("Invalid 'userLocale': null.");
- }
- //
- this.userLocale = userLocale;
- }
-
- protected Locale getUserLocaleReference() {
- return this.userLocale;
- }
-
- public Locale getUserLocale() {
- return (Locale)this.getUserLocaleReference().clone();
- }
-
- /* Storage number format */
-
- protected void setStorageNumberFormat(String storageNumberFormat) {
- MNumberFilter.checkNumberFormat(storageNumberFormat);
- //
- this.storageNumberFormat = storageNumberFormat;
- }
-
- public String getStorageNumberFormat() {
- return this.storageNumberFormat;
- }
-
- /* Storage locale */
-
- protected void setStorageLocale(Locale storageLocale) {
- if (null == storageLocale) {
- throw new IllegalArgumentException("Invalid 'storageLocale': null.");
- }
- //
- this.storageLocale = storageLocale;
- }
-
- protected Locale getStorageLocaleReference() {
- return this.storageLocale;
- }
-
- public Locale getStorageLocale() {
- return (Locale)this.getStorageLocaleReference().clone();
- }
-
- /* Separators */
-
- public void setUserGroupingSeparator(char s) {
- this.userGroupingSeparator = s;
- }
-
- public char getUserGroupingSeparator() {
- return this.userGroupingSeparator;
- }
-
- public void setUserDecimalSeparator(char s) {
- this.userDecimalSeparator = s;
- }
-
- public char getUserDecimalSeparator() {
- return this.userDecimalSeparator;
- }
-
- public void setStorageGroupingSeparator(char s) {
- this.storageGroupingSeparator = s;
- }
-
- public char getStorageGroupingSeparator() {
- return this.storageGroupingSeparator;
- }
-
- public void setStorageDecimalSeparator(char s) {
- this.storageDecimalSeparator = s;
- }
-
- public char getStorageDecimalSeparator() {
- return this.storageDecimalSeparator;
- }
-
- /* Number-specific methods */
-
- protected static void checkNumberFormat(String numberFormat) {
- if ((null == numberFormat) || ("".equals(numberFormat))) {
- throw new IllegalArgumentException("Invalid 'numberFormat': null or empty.");
- }
- //
- try {
- DecimalFormat testNumberFormat = new DecimalFormat(numberFormat);
- }
- catch (IllegalArgumentException exception) {
- throw new IllegalArgumentException(String.format("Invalid 'numberFormat': %s.", numberFormat)); // no need to propagate exception
- }
- }
-
- protected static LinkedHashMap<String, String> getNumberConstraintMap(String constraints) {
- LinkedHashSet<String> validConstraints = new LinkedHashSet<String>();
- validConstraints.add("notNull");
- validConstraints.add("minimumValue");
- validConstraints.add("maximumValue");
- return MNumberFilter.getConstraintMap(constraints, validConstraints);
- }
-
- protected static BigDecimal getNumberObject(String x, String inputNumberFormat, Locale inputLocale, char inputGroupingSeparator, char inputDecimalSeparator) throws MFormatFilteringException {
- if ((null == x) || ("".equals(x))) {
- throw new IllegalArgumentException("Invalid 'x': null or empty.");
- }
- MNumberFilter.checkNumberFormat(inputNumberFormat);
- if (null == inputLocale) {
- throw new IllegalArgumentException("Invalid 'inputLocale': null.");
- }
- //
- DecimalFormatSymbols dfs = new DecimalFormatSymbols(inputLocale);
- if (0 != inputGroupingSeparator) {
- dfs.setGroupingSeparator(inputGroupingSeparator);
- }
- if (0 != inputDecimalSeparator) {
- dfs.setDecimalSeparator(inputDecimalSeparator);
- }
- DecimalFormat df = new DecimalFormat(inputNumberFormat, dfs);
- df.setParseBigDecimal(true);
- ParsePosition validPosition = new ParsePosition(0);
- BigDecimal bd = (BigDecimal)df.parse(x, validPosition);
- if (validPosition.getIndex() < x.length()) {
- throw new MFormatFilteringException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputNumberFormat));
- }
- return bd;
- }
-
- protected static String getNumberString(BigDecimal number, String outputNumberFormat, Locale outputLocale, char outputGroupingSeparator, char outputDecimalSeparator) {
- if (null == number) {
- throw new IllegalArgumentException("Invalid 'number': null.");
- }
- MNumberFilter.checkNumberFormat(outputNumberFormat);
- if (null == outputLocale) {
- throw new IllegalArgumentException("Invalid 'outputLocale': null.");
- }
- //
- DecimalFormatSymbols dfs = new DecimalFormatSymbols(outputLocale);
- if (0 != outputGroupingSeparator) {
- dfs.setGroupingSeparator(outputGroupingSeparator);
- }
- if (0 != outputDecimalSeparator) {
- dfs.setDecimalSeparator(outputDecimalSeparator);
- }
- DecimalFormat df = new DecimalFormat(outputNumberFormat, dfs);
- return df.format(number);
- }
-
- protected String getRebuiltNumberString(String x, String inputNumberFormat, Locale inputLocale, char inputGroupingSeparator, char inputDecimalSeparator, String outputNumberFormat, Locale outputLocale, char outputGroupingSeparator, char outputDecimalSeparator, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- LinkedHashMap<String, String> constraintMap = MNumberFilter.getNumberConstraintMap(constraints);
- if ((null == x) || ("".equals(x))) {
- if ("true".equals(constraintMap.get("notNull"))) {
- throw new MConstraintFilteringException("Invalid 'x': cannot be null or empty.");
- }
- return "";
- }
- BigDecimal number = MNumberFilter.getNumberObject(x, inputNumberFormat, inputLocale, inputGroupingSeparator, inputDecimalSeparator);
- if (constraintMap.containsKey("minimumValue")) {
- BigDecimal constraintNumber = MNumberFilter.getNumberObject(constraintMap.get("minimumValue"), this.getStorageNumberFormat(), this.getStorageLocaleReference(), this.getStorageGroupingSeparator(), this.getStorageDecimalSeparator());
- if (number.compareTo(constraintNumber) < 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be >= %s.", x, constraintMap.get("minimumValue")));
- }
- }
- if (constraintMap.containsKey("maximumValue")) {
- BigDecimal constraintNumber = MNumberFilter.getNumberObject(constraintMap.get("maximumValue"), this.getStorageNumberFormat(), this.getStorageLocaleReference(), this.getStorageGroupingSeparator(), this.getStorageDecimalSeparator());
- if (number.compareTo(constraintNumber) > 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be <= %s.", x, constraintMap.get("maximumValue")));
- }
- }
- return MNumberFilter.getNumberString(number, outputNumberFormat, outputLocale, outputGroupingSeparator, outputDecimalSeparator);
- }
-
- public String getValidatedUserNumber(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userNumberFormat: this.getUserNumberFormatsReference()) {
- try {
- y = this.getRebuiltNumberString(x, userNumberFormat, this.getUserLocaleReference(), this.getUserGroupingSeparator(), this.getUserDecimalSeparator(), this.getDefaultUserNumberFormat(), this.getUserLocaleReference(), this.getUserGroupingSeparator(), this.getUserDecimalSeparator(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getStorageNumberFromUserNumber(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userNumberFormat: this.getUserNumberFormatsReference()) {
- try {
- y = this.getRebuiltNumberString(x, userNumberFormat, this.getUserLocaleReference(), this.getUserGroupingSeparator(), this.getUserDecimalSeparator(), this.getStorageNumberFormat(), this.getStorageLocaleReference(), this.getStorageGroupingSeparator(), this.getStorageDecimalSeparator(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getValidatedStorageNumber(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltNumberString(x, this.getStorageNumberFormat(), this.getStorageLocaleReference(), this.getStorageGroupingSeparator(), this.getStorageDecimalSeparator(), this.getStorageNumberFormat(), this.getStorageLocaleReference(), this.getStorageGroupingSeparator(), this.getStorageDecimalSeparator(), constraints);
- }
-
- public String getUserNumberFromStorageNumber(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltNumberString(x, this.getStorageNumberFormat(), this.getStorageLocaleReference(), this.getStorageGroupingSeparator(), this.getStorageDecimalSeparator(), this.getDefaultUserNumberFormat(), this.getUserLocaleReference(), this.getUserGroupingSeparator(), this.getUserDecimalSeparator(), constraints);
- }
-
- public BigDecimal getNumberObjectFromStorageNumber(String x) throws MFormatFilteringException {
- return MNumberFilter.getNumberObject(x, this.getStorageNumberFormat(), this.getStorageLocaleReference(), this.getStorageGroupingSeparator(), this.getStorageDecimalSeparator());
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-
-public class MSetElementFilter extends MFilter {
-
- /* */
-
- public MSetElementFilter() {
- super();
- }
-
- public MSetElementFilter clone() {
- return new MSetElementFilter();
- }
-
- /* Set element-specific methods */
-
- protected static LinkedHashMap<String, String> getSetElementConstraintMap(String constraints) {
- LinkedHashSet<String> validConstraints = new LinkedHashSet<String>();
- validConstraints.add("notNull");
- return MFilter.getConstraintMap(constraints, validConstraints);
- }
-
- public String getValidatedUserSetElement(String x, LinkedHashSet<String> set, String constraints) throws MConstraintFilteringException {
- LinkedHashMap<String, String> constraintMap = MSetElementFilter.getSetElementConstraintMap(constraints);
- if (("true".equals(constraintMap.get("notNull"))) && (null == x)) {
- throw new MConstraintFilteringException("Invalid 'x': cannot be null.");
- }
- if (null != x) {
- if (null == set) {
- throw new IllegalArgumentException("Invalid 'set': null.");
- }
- if (!set.contains(x)) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: not in set.", x));
- }
- }
- return x;
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-
-public class MTextFilter extends MFilter {
-
- /* */
-
- public MTextFilter() {
- super();
- }
-
- public MTextFilter clone() {
- return new MTextFilter();
- }
-
- /* Text-specific methods */
-
- protected static LinkedHashMap<String, String> getTextConstraintMap(String constraints) {
- LinkedHashSet<String> validConstraints = new LinkedHashSet<String>();
- validConstraints.add("notNull");
- validConstraints.add("minimumLength");
- validConstraints.add("maximumLength");
- return MFilter.getConstraintMap(constraints, validConstraints);
- }
-
- public String getValidatedUserText(String x, String constraints) throws MConstraintFilteringException {
- LinkedHashMap<String, String> constraintMap = MTextFilter.getTextConstraintMap(constraints);
- if (("true".equals(constraintMap.get("notNull"))) && (null == x)) {
- throw new MConstraintFilteringException("Invalid 'x': cannot be null.");
- }
- if (null != x) {
- if (constraintMap.containsKey("minimumLength")) {
- if (x.codePointCount(0, x.length()) < Integer.parseInt(constraintMap.get("minimumLength"))) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: length must be >= %s.", x, constraintMap.get("minimumLength")));
- }
- }
- if (constraintMap.containsKey("maximumLength")) {
- if (x.codePointCount(0, x.length()) > Integer.parseInt(constraintMap.get("maximumLength"))) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: length must be <= %s.", x, constraintMap.get("maximumLength")));
- }
- }
- }
- return x;
- }
-
-}
+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.filtering;
-
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.Locale;
-import java.util.TimeZone;
-
-public class MTimeFilter extends MFilter {
-
- protected LinkedHashSet<String> userTimeFormats = new LinkedHashSet<String>();
- protected Locale userLocale = null;
- protected TimeZone userTimeZone = null;
- protected String storageTimeFormat = null;
- protected Locale storageLocale = null;
- protected TimeZone storageTimeZone = null;
-
- /* */
-
- public MTimeFilter(String defaultUserTimeFormat, Locale userLocale, String storageTimeFormat, Locale storageLocale) {
- this(defaultUserTimeFormat, userLocale, TimeZone.getTimeZone("UTC"), storageTimeFormat, storageLocale, TimeZone.getTimeZone("UTC"));
- }
-
- public MTimeFilter(String defaultUserTimeFormat, Locale userLocale, TimeZone userTimeZone, String storageTimeFormat, Locale storageLocale, TimeZone storageTimeZone) {
- super();
- //
- this.addUserTimeFormat(defaultUserTimeFormat);
- this.setUserLocale(userLocale);
- this.setUserTimeZone(userTimeZone);
- this.setStorageTimeFormat(storageTimeFormat);
- this.setStorageLocale(storageLocale);
- this.setStorageTimeZone(storageTimeZone);
- }
-
- public MTimeFilter(LinkedHashSet<String> userTimeFormats, Locale userLocale, TimeZone userTimeZone, String storageTimeFormat, Locale storageLocale, TimeZone storageTimeZone) {
- super();
- //
- this.setUserTimeFormats(userTimeFormats);
- this.setUserLocale(userLocale);
- this.setUserTimeZone(userTimeZone);
- this.setStorageTimeFormat(storageTimeFormat);
- this.setStorageLocale(storageLocale);
- this.setStorageTimeZone(storageTimeZone);
- }
-
- public MTimeFilter clone() {
- return new MTimeFilter(this.getUserTimeFormats(), this.getUserLocale(), this.getUserTimeZone(), this.getStorageTimeFormat(), this.getStorageLocale(), this.getStorageTimeZone());
- }
-
- /* User time formats */
-
- public void setUserTimeFormats(LinkedHashSet<String> userTimeFormats) {
- if (null == userTimeFormats) {
- throw new IllegalArgumentException("Invalid 'userTimeFormats': null.");
- }
- else {
- Iterator i = userTimeFormats.iterator();
- while (i.hasNext()) {
- MTimeFilter.checkTimeFormat((String)i.next());
- }
- }
- //
- synchronized (this.userTimeFormats) {
- this.userTimeFormats = userTimeFormats;
- }
- }
-
- public void addUserTimeFormat(String userTimeFormat) {
- MTimeFilter.checkTimeFormat(userTimeFormat);
- //
- synchronized (this.userTimeFormats) {
- this.userTimeFormats.add(userTimeFormat);
- }
- }
-
- protected LinkedHashSet<String> getUserTimeFormatsReference() {
- return this.userTimeFormats;
- }
-
- public LinkedHashSet<String> getUserTimeFormats() {
- LinkedHashSet<String> tmpUserTimeFormats = new LinkedHashSet<String>();
- Iterator i = this.getUserTimeFormatsReference().iterator();
- while (i.hasNext()) {
- tmpUserTimeFormats.add((String)i.next());
- }
- return tmpUserTimeFormats;
- }
-
- public String getDefaultUserTimeFormat() {
- return this.getUserTimeFormatsReference().iterator().next();
- }
-
- /* User locale */
-
- protected void setUserLocale(Locale userLocale) {
- if (null == userLocale) {
- throw new IllegalArgumentException("Invalid 'userLocale': null.");
- }
- //
- this.userLocale = userLocale;
- }
-
- protected Locale getUserLocaleReference() {
- return this.userLocale;
- }
-
- public Locale getUserLocale() {
- return (Locale)this.getUserLocaleReference().clone();
- }
-
- /* User time zone */
-
- protected void setUserTimeZone(TimeZone userTimeZone) {
- if (null == userTimeZone) {
- throw new IllegalArgumentException("Invalid 'userTimeZone': null.");
- }
- //
- this.userTimeZone = userTimeZone;
- }
-
- protected TimeZone getUserTimeZoneReference() {
- return this.userTimeZone;
- }
-
- public TimeZone getUserTimeZone() {
- return (TimeZone)this.getUserTimeZoneReference().clone();
- }
-
- /* Storage time format */
-
- protected void setStorageTimeFormat(String storageTimeFormat) {
- MTimeFilter.checkTimeFormat(storageTimeFormat);
- //
- this.storageTimeFormat = storageTimeFormat;
- }
-
- public String getStorageTimeFormat() {
- return this.storageTimeFormat;
- }
-
- /* Storage locale */
-
- protected void setStorageLocale(Locale storageLocale) {
- if (null == storageLocale) {
- throw new IllegalArgumentException("Invalid 'storageLocale': null.");
- }
- //
- this.storageLocale = storageLocale;
- }
-
- protected Locale getStorageLocaleReference() {
- return this.storageLocale;
- }
-
- public Locale getStorageLocale() {
- return (Locale)this.getStorageLocaleReference().clone();
- }
-
- /* Storage time zone */
-
- protected void setStorageTimeZone(TimeZone storageTimeZone) {
- if (null == storageTimeZone) {
- throw new IllegalArgumentException("Invalid 'storageTimeZone': null.");
- }
- //
- this.storageTimeZone = storageTimeZone;
- }
-
- protected TimeZone getStorageTimeZoneReference() {
- return this.storageTimeZone;
- }
-
- public TimeZone getStorageTimeZone() {
- return (TimeZone)this.getStorageTimeZoneReference().clone();
- }
-
- /* Time-specific methods */
-
- protected static void checkTimeFormat(String timeFormat) {
- if ((null == timeFormat) || ("".equals(timeFormat))) {
- throw new IllegalArgumentException("Invalid 'timeFormat': null or empty.");
- }
- //
- try {
- SimpleDateFormat testTimeFormat = new SimpleDateFormat(timeFormat);
- }
- catch (IllegalArgumentException exception) {
- throw new IllegalArgumentException(String.format("Invalid 'timeFormat': %s.", timeFormat)); // no need to propagate exception
- }
- }
-
- protected static LinkedHashMap<String, String> getTimeConstraintMap(String constraints) {
- LinkedHashSet<String> validConstraints = new LinkedHashSet<String>();
- validConstraints.add("notNull");
- validConstraints.add("minimumValue");
- validConstraints.add("maximumValue");
- return MTimeFilter.getConstraintMap(constraints, validConstraints);
- }
-
- protected static Date getTimeObject(String x, String inputTimeFormat, Locale inputLocale, TimeZone inputTimeZone) throws MFormatFilteringException {
- if ((null == x) || ("".equals(x))) {
- throw new IllegalArgumentException("Invalid 'x': null or empty.");
- }
- MTimeFilter.checkTimeFormat(inputTimeFormat);
- if (null == inputLocale) {
- throw new IllegalArgumentException("Invalid 'inputLocale': null.");
- }
- if (null == inputTimeZone) {
- throw new IllegalArgumentException("Invalid 'inputTimeZone': null.");
- }
- //
- Calendar c1 = Calendar.getInstance(inputTimeZone, inputLocale);
- c1.clear();
- c1.setLenient(false);
- SimpleDateFormat sdf = new SimpleDateFormat(inputTimeFormat, inputLocale);
- sdf.setCalendar(c1);
- Date d1 = null;
- try {
- d1 = sdf.parse(x);
- }
- catch (ParseException exception) {
- throw new MFormatFilteringException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputTimeFormat)); // no need to propagate exception
- }
- Calendar c2 = Calendar.getInstance(inputTimeZone, inputLocale);
- c2.clear();
- c2.set(Calendar.HOUR_OF_DAY, c1.get(Calendar.HOUR_OF_DAY));
- c2.set(Calendar.MINUTE, c1.get(Calendar.MINUTE));
- c2.set(Calendar.SECOND, c1.get(Calendar.SECOND));
- Date d2 = c2.getTime();
- if (!x.equals(sdf.format(d2))) {
- throw new MFormatFilteringException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputTimeFormat));
- }
- return d2;
- }
-
- protected static String getTimeString(Date time, String outputTimeFormat, Locale outputLocale, TimeZone outputTimeZone) {
- if (null == time) {
- throw new IllegalArgumentException("Invalid 'time': null.");
- }
- MTimeFilter.checkTimeFormat(outputTimeFormat);
- if (null == outputLocale) {
- throw new IllegalArgumentException("Invalid 'outputLocale': null.");
- }
- if (null == outputTimeZone) {
- throw new IllegalArgumentException("Invalid 'outputTimeZone': null.");
- }
- //
- SimpleDateFormat sdf = new SimpleDateFormat(outputTimeFormat, outputLocale);
- sdf.setCalendar(Calendar.getInstance(outputTimeZone, outputLocale));
- return sdf.format(time);
- }
-
- protected String getRebuiltTimeString(String x, String inputTimeFormat, Locale inputLocale, TimeZone inputTimeZone, String outputTimeFormat, Locale outputLocale, TimeZone outputTimeZone, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- LinkedHashMap<String, String> constraintMap = MTimeFilter.getTimeConstraintMap(constraints);
- if ((null == x) || ("".equals(x))) {
- if ("true".equals(constraintMap.get("notNull"))) {
- throw new MConstraintFilteringException("Invalid 'x': cannot be null or empty.");
- }
- return "";
- }
- Date time = MTimeFilter.getTimeObject(x, inputTimeFormat, inputLocale, inputTimeZone);
- if (constraintMap.containsKey("minimumValue")) {
- Date constraintTime = MTimeFilter.getTimeObject(constraintMap.get("minimumValue"), this.getStorageTimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- if (time.compareTo(constraintTime) < 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be >= %s.", x, constraintMap.get("minimumValue")));
- }
- }
- if (constraintMap.containsKey("maximumValue")) {
- Date constraintTime = MTimeFilter.getTimeObject(constraintMap.get("maximumValue"), this.getStorageTimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- if (time.compareTo(constraintTime) > 0) {
- throw new MConstraintFilteringException(String.format("Invalid 'x': %s: value must be <= %s.", x, constraintMap.get("maximumValue")));
- }
- }
- return MTimeFilter.getTimeString(time, outputTimeFormat, outputLocale, outputTimeZone);
- }
-
- public String getValidatedUserTime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userTimeFormat: this.getUserTimeFormatsReference()) {
- try {
- y = this.getRebuiltTimeString(x, userTimeFormat, this.getUserLocaleReference(), this.getUserTimeZoneReference(), this.getDefaultUserTimeFormat(), this.getUserLocaleReference(), this.getUserTimeZoneReference(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getStorageTimeFromUserTime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- String y = null;
- for (String userTimeFormat: this.getUserTimeFormatsReference()) {
- try {
- y = this.getRebuiltTimeString(x, userTimeFormat, this.getUserLocaleReference(), this.getUserTimeZoneReference(), this.getStorageTimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), constraints);
- return y;
- }
- catch (MFormatFilteringException exception) {
- }
- }
- if (null == y) {
- throw new MFormatFilteringException(String.format("Invalid 'x': %s.", x));
- }
- return y; // necessary to avoid Java compilation errors
- }
-
- public String getValidatedStorageTime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltTimeString(x, this.getStorageTimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), this.getStorageTimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), constraints);
- }
-
- public String getUserTimeFromStorageTime(String x, String constraints) throws MConstraintFilteringException, MFormatFilteringException {
- return this.getRebuiltTimeString(x, this.getStorageTimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference(), this.getDefaultUserTimeFormat(), this.getUserLocaleReference(), this.getUserTimeZoneReference(), constraints);
- }
-
- public Date getTimeObjectFromStorageTime(String x) throws MFormatFilteringException {
- return MTimeFilter.getTimeObject(x, this.getStorageTimeFormat(), this.getStorageLocaleReference(), this.getStorageTimeZoneReference());
- }
-
-}