Skip to content

Commit

Permalink
add jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
UrielCh committed Mar 18, 2024
1 parent e62420e commit 123e907
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public boolean isWritable() {
* @param c the handled class
* @param field the field to access
* @param filter field filter
*
*/
public Accessor(Class<?> c, Field field, FieldFilter filter) {
this.fieldName = field.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,58 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* A basic implementation of the {@link FieldFilter} interface that permits all operations on fields.
* This implementation returns {@code true} for all checks, indicating that any field can be used, read, and written.
* It serves as a default or fallback strategy when no specific field filtering logic is required.
*/
public class BasicFiledFilter implements FieldFilter {
/**
* A singleton instance of {@code BasicFieldFilter}.
* Since the filter does not maintain any state and allows all operations, it can be reused across the application.
*/
public final static BasicFiledFilter SINGLETON = new BasicFiledFilter();

/**
* Always allows using the specified field.
*
* @param field The field to check.
* @return Always returns {@code true}.
*/
@Override
public boolean canUse(Field field) {
return true;
}

/**
* Always allows using the specified field in conjunction with a method.
*
* @param field The field to check.
* @param method The method to check. This parameter is not used in the current implementation.
* @return Always returns {@code true}.
*/
@Override
public boolean canUse(Field field, Method method) {
return true;
}

/**
* Always allows reading the specified field.
*
* @param field The field to check.
* @return Always returns {@code true}.
*/
@Override
public boolean canRead(Field field) {
return true;
}

/**
* Always allows writing to the specified field.
*
* @param field The field to check.
* @return Always returns {@code true}.
*/
@Override
public boolean canWrite(Field field) {
return true;
Expand Down
17 changes: 15 additions & 2 deletions accessors-smart/src/main/java/net/minidev/asm/ConvertDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,34 @@
import java.util.TimeZone;
import java.util.TreeMap;

/**
* Utility class for converting strings into {@link Date} objects, considering various global date formats.
* It handles different month and day names across languages, and supports timezone adjustments.
*/
public class ConvertDate {
static TreeMap<String, Integer> monthsTable = new TreeMap<String, Integer>(new StringCmpNS()); // StringCmpNS.COMP
static TreeMap<String, Integer> daysTable = new TreeMap<String, Integer>(new StringCmpNS()); // StringCmpNS.COMP
private static HashSet<String> voidData = new HashSet<String>();
/**
* overwrite system time zone
*/
* Default TimeZone used for date conversions. Can be overwritten to change the default time zone.
*/
public static TimeZone defaultTimeZone;
/**
* Comparator for case-insensitive string comparison. Used for sorting and comparing month and day names.
*/
public static class StringCmpNS implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);
}
}

/**
* Retrieves the month's integer representation based on the provided month name.
*
* @param month the name of the month
* @return the integer value of the month, or null if the month name is unrecognized
*/
public static Integer getMonth(String month) {
return monthsTable.get(month);
}
Expand Down
118 changes: 118 additions & 0 deletions accessors-smart/src/main/java/net/minidev/asm/DefaultConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@

import net.minidev.asm.ex.ConvertException;

/**
* Provides utility methods to convert objects to different primitive types and their wrapper classes.
* It supports conversion from {@link Number} instances and {@link String} representations of numbers
* to their corresponding primitive types or wrapper classes. Conversion from types that are not supported
* will result in a {@link ConvertException}.
*/
public class DefaultConverter {
/**
* Converts the given object to an {@code int}.
*
* @param obj the object to convert
* @return the converted int value, or 0 if the object is {@code null}
* @throws ConvertException if the object cannot be converted to an int
*/
public static int convertToint(Object obj) {
if (obj == null)
return 0;
Expand All @@ -13,6 +26,13 @@ public static int convertToint(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to int");
}

/**
* Converts the given object to an {@link Integer}.
*
* @param obj the object to convert
* @return the converted Integer, or {@code null} if the object is {@code null}
* @throws ConvertException if the object cannot be converted to an Integer
*/
public static Integer convertToInt(Object obj) {
if (obj == null)
return null;
Expand All @@ -24,6 +44,13 @@ public static Integer convertToInt(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to Integer");
}

/**
* Converts the given object to a {@code short}.
*
* @param obj the object to convert
* @return the converted short value, or 0 if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a short
*/
public static short convertToshort(Object obj) {
if (obj == null)
return 0;
Expand All @@ -34,6 +61,13 @@ public static short convertToshort(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to short");
}

/**
* Converts the given object to a {@code short}.
*
* @param obj the object to convert
* @return the converted short value, or 0 if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a short
*/
public static Short convertToShort(Object obj) {
if (obj == null)
return null;
Expand All @@ -45,6 +79,13 @@ public static Short convertToShort(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to Short");
}

/**
* Converts the given object to a {@code long}.
*
* @param obj the object to convert
* @return the converted long value, or 0 if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a long
*/
public static long convertTolong(Object obj) {
if (obj == null)
return 0;
Expand All @@ -55,6 +96,13 @@ public static long convertTolong(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to long");
}

/**
* Converts the given object to a {@link Long}.
*
* @param obj the object to convert
* @return the converted Long, or {@code null} if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a Long
*/
public static Long convertToLong(Object obj) {
if (obj == null)
return null;
Expand All @@ -66,6 +114,13 @@ public static Long convertToLong(Object obj) {
throw new ConvertException("Primitive: Can not convert value '" + obj+ "' As " + obj.getClass().getName() + " to Long");
}

/**
* Converts the given object to a {@code byte}.
*
* @param obj the object to convert
* @return the converted byte value, or 0 if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a byte
*/
public static byte convertTobyte(Object obj) {
if (obj == null)
return 0;
Expand All @@ -76,6 +131,13 @@ public static byte convertTobyte(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to byte");
}

/**
* Converts the given object to a {@link Byte}.
*
* @param obj the object to convert
* @return the converted Byte, or {@code null} if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a Byte
*/
public static Byte convertToByte(Object obj) {
if (obj == null)
return null;
Expand All @@ -87,6 +149,13 @@ public static Byte convertToByte(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to Byte");
}

/**
* Converts the given object to a {@code float}.
*
* @param obj the object to convert
* @return the converted float value, or 0f if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a float
*/
public static float convertTofloat(Object obj) {
if (obj == null)
return 0f;
Expand All @@ -97,6 +166,13 @@ public static float convertTofloat(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to float");
}

/**
* Converts the given object to a {@link Byte}.
*
* @param obj the object to convert
* @return the converted Byte, or {@code null} if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a Byte
*/
public static Float convertToFloat(Object obj) {
if (obj == null)
return null;
Expand All @@ -108,6 +184,13 @@ public static Float convertToFloat(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to Float");
}

/**
* Converts the given object to a {@code double}.
*
* @param obj the object to convert
* @return the converted double value, or 0.0 if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a double
*/
public static double convertTodouble(Object obj) {
if (obj == null)
return 0.0;
Expand All @@ -118,6 +201,13 @@ public static double convertTodouble(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to float");
}

/**
* Converts the given object to a {@link Double}.
*
* @param obj the object to convert
* @return the converted Double, or {@code null} if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a Double
*/
public static Double convertToDouble(Object obj) {
if (obj == null)
return null;
Expand All @@ -129,6 +219,13 @@ public static Double convertToDouble(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to Float");
}

/**
* Converts the given object to a {@code char}.
*
* @param obj the object to convert
* @return the converted char value, or a space character if the object is {@code null} or the string is empty
* @throws ConvertException if the object cannot be converted to a char
*/
public static char convertTochar(Object obj) {
if (obj == null)
return ' ';
Expand All @@ -140,6 +237,13 @@ public static char convertTochar(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to char");
}

/**
* Converts the given object to a {@link Character}.
*
* @param obj the object to convert
* @return the converted Character, or {@code null} if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a Character
*/
public static Character convertToChar(Object obj) {
if (obj == null)
return null;
Expand All @@ -154,6 +258,13 @@ public static Character convertToChar(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to Character");
}

/**
* Converts the given object to a {@code boolean}.
*
* @param obj the object to convert
* @return the converted boolean value, false if the object is {@code null} or represents the numeric value 0
* @throws ConvertException if the object cannot be converted to a boolean
*/
public static boolean convertTobool(Object obj) {
if (obj == null)
return false;
Expand All @@ -170,6 +281,13 @@ public static boolean convertTobool(Object obj) {
throw new ConvertException("Primitive: Can not convert " + obj.getClass().getName() + " to boolean");
}

/**
* Converts the given object to a {@link Boolean}.
*
* @param obj the object to convert
* @return the converted Boolean, or {@code null} if the object is {@code null}
* @throws ConvertException if the object cannot be converted to a Boolean
*/
public static Boolean convertToBool(Object obj) {
if (obj == null)
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package net.minidev.asm.ex;

/**
* An exception that is thrown to indicate a problem occurred during a conversion process.
* This class extends {@link RuntimeException} and is used to signify errors encountered
* while converting between types, typically within a dynamic type conversion framework or library.
*/
public class ConvertException extends RuntimeException {
private static final long serialVersionUID = 1L;

/**
* Constructs a new {@code ConvertException} with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
*/
public ConvertException() {
super();
}


/**
* Constructs a new {@code ConvertException} with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method.
*/
public ConvertException(String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public NoSuchFieldException() {
super();
}

/**
* @param message the detail message. The detail message is saved for
* later retrieval by the Throwable.getMessage() method.
*/
public NoSuchFieldException(String message) {
super(message);
}
Expand Down

0 comments on commit 123e907

Please sign in to comment.