Package io.jsondb

Class Util


  • public class Util
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Util()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.lang.Object deepCopy​(java.lang.Object fromBean)
      A utility method that creates a deep clone of the specified object.
      static void delete​(java.io.File f)
      Utility to delete directory recursively
      protected static java.lang.String determineCollectionName​(java.lang.Class<?> entityClass)
      A utility method to determine the collection name for a given entity class.
      protected static <T> java.lang.String determineEntityCollectionName​(T obj)  
      protected static void ensureNotRestricted​(java.lang.Object o)  
      protected static java.lang.Object getIdForEntity​(java.lang.Object document, java.lang.reflect.Method getterMethodForId)
      A utility method to extract the value of field marked by the @Id annotation using its getter/accessor method.
      static java.util.List<java.lang.Integer> getSliceIndexes​(java.lang.String slice, int size)
      Utility method to compute the indexes to select based on slice string
      static boolean isSliceable​(java.lang.String slice)  
      protected static java.lang.Object setFieldValueForEntity​(java.lang.Object document, java.lang.Object newValue, java.lang.reflect.Method setterMethod)  
      protected static java.lang.Object setIdForEntity​(java.lang.Object document, java.lang.reflect.Method setterMethodForId)
      A utility method to set the value of field marked by the @Id annotation using its setter/mutator method.
      static boolean stampVersion​(JsonDBConfig dbConfig, java.io.File f, java.lang.String version)
      Utility to stamp the version into a newly created .json File This method is expected to be invoked on a newly created .json file before it is usable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • ensureNotRestricted

        protected static void ensureNotRestricted​(java.lang.Object o)
      • determineEntityCollectionName

        protected static <T> java.lang.String determineEntityCollectionName​(T obj)
      • determineCollectionName

        protected static java.lang.String determineCollectionName​(java.lang.Class<?> entityClass)
        A utility method to determine the collection name for a given entity class. This method attempts to find a the annotation Document on this class. If found then we know the collection name else it throws a exception
        Parameters:
        entityClass - class that determines the name of the collection
        Returns:
        name of the class
      • getIdForEntity

        protected static java.lang.Object getIdForEntity​(java.lang.Object document,
                                                         java.lang.reflect.Method getterMethodForId)
        A utility method to extract the value of field marked by the @Id annotation using its getter/accessor method.
        Parameters:
        document - the actual Object representing the POJO we want the Id of.
        getterMethodForId - the Method that is the accessor for the attributed with @Id annotation
        Returns:
        the actual Id or if none exists then a new random UUID
      • setIdForEntity

        protected static java.lang.Object setIdForEntity​(java.lang.Object document,
                                                         java.lang.reflect.Method setterMethodForId)
        A utility method to set the value of field marked by the @Id annotation using its setter/mutator method. TODO: Some day we want to support policies for generation of ID like AutoIncrement etc.
        Parameters:
        document - the actual Object representing the POJO we want the Id to be set for.
        setterMethodForId - the Method that is the mutator for the attributed with @Id annotation
        Returns:
        the Id that was generated and set
      • setFieldValueForEntity

        protected static java.lang.Object setFieldValueForEntity​(java.lang.Object document,
                                                                 java.lang.Object newValue,
                                                                 java.lang.reflect.Method setterMethod)
      • deepCopy

        protected static java.lang.Object deepCopy​(java.lang.Object fromBean)
        A utility method that creates a deep clone of the specified object. There is no other way of doing this reliably.
        Parameters:
        fromBean - java bean to be cloned.
        Returns:
        a new java bean cloned from fromBean.
      • stampVersion

        public static boolean stampVersion​(JsonDBConfig dbConfig,
                                           java.io.File f,
                                           java.lang.String version)
        Utility to stamp the version into a newly created .json File This method is expected to be invoked on a newly created .json file before it is usable. So no locking code required.
        Parameters:
        dbConfig - all the settings used by Json DB
        f - the target .json file on which to stamp the version
        version - the actual version string to stamp
        Returns:
        true if success.
      • delete

        public static void delete​(java.io.File f)
        Utility to delete directory recursively
        Parameters:
        f - File object representing the directory to recursively delete
      • isSliceable

        public static boolean isSliceable​(java.lang.String slice)
      • getSliceIndexes

        public static java.util.List<java.lang.Integer> getSliceIndexes​(java.lang.String slice,
                                                                        int size)
        Utility method to compute the indexes to select based on slice string
        Parameters:
        slice - select the indices in some slice_target list or array, should be a valid slice string The behaviour of this slicing feature is similar to the slicing feature in python or numpy, as much as possible https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html A slice is a string notation and the basic slice syntax is i:j:k, where i is the starting index, j is the stopping index, and k is the step (k != 0). In other words it is start:stop:step Example slice_target = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] slice = "1:7:2" returns [T1, T3, T5] i is inclusive, j is exclusive Negative i and j are interpreted as n + i and n + j where n is the number of elements found. Negative k makes stepping go towards smaller indices. Example slice_target = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] slice = "-2:10" returns [T8, T9] slice = "-3:3:-1" returns [T7, T6, T5, T4] Assume n is the number of elements in slice_target. Then, if i is not given it defaults to 0 for k>0 and n - 1 for k<0 . If j is not given it defaults to n for k>0 and -n-1 for k<0 . If k is not given it defaults to 1. Note that :: is the same as : and means select all indices from slice_target. Example slice_target = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] slice = "5:" returns [T5, T6, T7, T8, T9] Assume n is the number of elements in slice_target. Then, if j>n then it is considered as n, in case of negative j it is considered -n.
        size - size of the array from which a slice is to extracted
        Returns:
        List of indexes to pick from the array to be returned