Android Open Source - LitePal Base Utility






From Project

Back to project page LitePal.

License

The source code is released under:

Apache License

If you think the Android project LitePal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C)  Tony Green, Litepal Framework Open Source Project
 *// w  w  w  . j  av a  2s .c om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.litepal.util;

import java.util.Collection;
import java.util.Locale;

import org.litepal.exceptions.DataSupportException;
import org.litepal.parser.LitePalAttr;

import android.text.TextUtils;

/**
 * A utility class to help LitePal with some base actions that might through any
 * components. These actions can help classes just do the jobs they care, and
 * help them out of the trivial work.
 * 
 * @author Tony Green
 * @since 1.0
 */
public class BaseUtility {

  /**
   * Disable to create an instance of BaseUtility.
   */
  private BaseUtility() {
  }

  /**
   * It will change the case of the passing parameter into the case defined in
   * litepal.xml file.
   * 
   * @param string
   *            The string want to change case.
   * @return The string after changing case. If the name is null, then simply
   *         return null.
   */
  public static String changeCase(String string) {
    if (string != null) {
      LitePalAttr litePalAttr = LitePalAttr.getInstance();
      String cases = litePalAttr.getCases();
      if (Const.LitePal.CASES_KEEP.equals(cases)) {
        return string;
      } else if (Const.LitePal.CASES_UPPER.equals(cases)) {
        return string.toUpperCase(Locale.getDefault());
      }
      return string.toLowerCase(Locale.getDefault());
    }
    return null;
  }

  /**
   * This helper method makes up the shortage of contains method in Collection
   * to support the function of case insensitive contains. It only supports
   * the String generic type of collection, cause other types have no cases
   * concept.
   * 
   * @param collection
   *            The collection contains string data.
   * @param string
   *            The string want to look for in the collection.
   * @return If the string is in the collection without case concern return
   *         true, otherwise return false. If the collection is null, return
   *         false.
   */
  public static boolean containsIgnoreCases(Collection<String> collection, String string) {
    if (collection == null) {
      return false;
    }
    if (string == null) {
      return collection.contains(null);
    }
    boolean contains = false;
    for (String element : collection) {
      if (string.equalsIgnoreCase(element)) {
        contains = true;
        break;
      }
    }
    return contains;
  }

  /**
   * Capitalize make the first letter of the word be upper case.
   * 
   * @param string
   *            The word to capitalize.
   * @return The word after capitalize.
   */
  public static String capitalize(String string) {
    if (!TextUtils.isEmpty(string)) {
      return string.substring(0, 1).toUpperCase(Locale.getDefault()) + string.substring(1);
    }
    return string == null ? null : "";
  }

  /**
   * Count how many marks existed in string.
   * 
   * @param string
   *            The source sentence.
   * @param mark
   *            The specific substring to count.
   * @return The number of marks existed in string.
   */
  public static int count(String string, String mark) {
    if (!TextUtils.isEmpty(string) && !TextUtils.isEmpty(mark)) {
      int count = 0;
      int index = string.indexOf(mark);
      while (index != -1) {
        count++;
        string = string.substring(index + mark.length());
        index = string.indexOf(mark);
      }
      return count;
    }
    return 0;
  }

  /**
   * Check the number of question mark existed in conditions[0] equals the
   * number of rest conditions elements or not. If not equals, throws
   * DataSupportException.
   * 
   * @param conditions
   *            A string array representing the WHERE part of an SQL
   *            statement.
   * @throws DataSupportException
   */
  public static void checkConditionsCorrect(String... conditions) {
    if (conditions != null) {
      int conditionsSize = conditions.length;
      if (conditionsSize > 0) {
        String whereClause = conditions[0];
        int placeHolderSize = BaseUtility.count(whereClause, "?");
        if (conditionsSize != placeHolderSize + 1) {
          throw new DataSupportException(DataSupportException.UPDATE_CONDITIONS_EXCEPTION);
        }
      }
    }
  }

  /**
   * Judge a field type is supported or not. Currently only basic data types
   * and String are supported.
   * 
   * @param fieldType
   *            Text field type.
   * @return Supported return true, not supported return false.
   */
  public static boolean isFieldTypeSupported(String fieldType) {
    if ("boolean".equals(fieldType) || "java.lang.Boolean".equals(fieldType)) {
      return true;
    }
    if ("float".equals(fieldType) || "java.lang.Float".equals(fieldType)) {
      return true;
    }
    if ("double".equals(fieldType) || "java.lang.Double".equals(fieldType)) {
      return true;
    }
    if ("int".equals(fieldType) || "java.lang.Integer".equals(fieldType)) {
      return true;
    }
    if ("long".equals(fieldType) || "java.lang.Long".equals(fieldType)) {
      return true;
    }
    if ("short".equals(fieldType) || "java.lang.Short".equals(fieldType)) {
      return true;
    }
    if ("char".equals(fieldType) || "java.lang.Character".equals(fieldType)) {
      return true;
    }
    if ("java.lang.String".equals(fieldType) || "java.util.Date".equals(fieldType)) {
      return true;
    }
    return false;
  }

}




Java Source Code List

org.litepal.LitePalApplication.java
org.litepal.LitePalBase.java
org.litepal.crud.AssociationsAnalyzer.java
org.litepal.crud.ClusterQuery.java
org.litepal.crud.DataHandler.java
org.litepal.crud.DataSupport.java
org.litepal.crud.DeleteHandler.java
org.litepal.crud.DynamicExecutor.java
org.litepal.crud.Many2ManyAnalyzer.java
org.litepal.crud.Many2OneAnalyzer.java
org.litepal.crud.One2OneAnalyzer.java
org.litepal.crud.QueryHandler.java
org.litepal.crud.SaveHandler.java
org.litepal.crud.UpdateHandler.java
org.litepal.crud.model.AssociationsInfo.java
org.litepal.exceptions.DataSupportException.java
org.litepal.exceptions.DatabaseGenerateException.java
org.litepal.exceptions.GlobalException.java
org.litepal.exceptions.InvalidAttributesException.java
org.litepal.exceptions.ParseConfigurationFileException.java
org.litepal.litepalsample.activity.AggregateActivity.java
org.litepal.litepalsample.activity.AverageSampleActivity.java
org.litepal.litepalsample.activity.CRUDActivity.java
org.litepal.litepalsample.activity.CountSampleActivity.java
org.litepal.litepalsample.activity.DeleteSampleActivity.java
org.litepal.litepalsample.activity.MainActivity.java
org.litepal.litepalsample.activity.ManageTablesActivity.java
org.litepal.litepalsample.activity.MaxSampleActivity.java
org.litepal.litepalsample.activity.MinSampleActivity.java
org.litepal.litepalsample.activity.ModelListActivity.java
org.litepal.litepalsample.activity.ModelStructureActivity.java
org.litepal.litepalsample.activity.QuerySampleActivity.java
org.litepal.litepalsample.activity.SaveSampleActivity.java
org.litepal.litepalsample.activity.SumSampleActivity.java
org.litepal.litepalsample.activity.TableListActivity.java
org.litepal.litepalsample.activity.TableStructureActivity.java
org.litepal.litepalsample.activity.UpdateSampleActivity.java
org.litepal.litepalsample.adapter.DataArrayAdapter.java
org.litepal.litepalsample.adapter.StringArrayAdapter.java
org.litepal.litepalsample.model.Album.java
org.litepal.litepalsample.model.Singer.java
org.litepal.litepalsample.model.Song.java
org.litepal.litepalsample.util.Utility.java
org.litepal.model.Table_Schema.java
org.litepal.parser.LitePalAttr.java
org.litepal.parser.LitePalContentHandler.java
org.litepal.parser.LitePalParser.java
org.litepal.tablemanager.AssociationCreator.java
org.litepal.tablemanager.AssociationUpdater.java
org.litepal.tablemanager.Connector.java
org.litepal.tablemanager.Creator.java
org.litepal.tablemanager.Dropper.java
org.litepal.tablemanager.Generator.java
org.litepal.tablemanager.LitePalOpenHelper.java
org.litepal.tablemanager.Upgrader.java
org.litepal.tablemanager.model.AssociationsModel.java
org.litepal.tablemanager.model.TableModel.java
org.litepal.tablemanager.typechange.BooleanOrm.java
org.litepal.tablemanager.typechange.DateOrm.java
org.litepal.tablemanager.typechange.DecimalOrm.java
org.litepal.tablemanager.typechange.NumericOrm.java
org.litepal.tablemanager.typechange.OrmChange.java
org.litepal.tablemanager.typechange.TextOrm.java
org.litepal.util.BaseUtility.java
org.litepal.util.Const.java
org.litepal.util.DBUtility.java
org.litepal.util.LogUtil.java
org.litepal.util.SharedUtil.java