Android Open Source - sugar Naming Helper






From Project

Back to project page sugar.

License

The source code is released under:

Copyright (C) 2012 by Satya Narayan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the S...

If you think the Android project sugar 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

package com.orm.util;
//from  w ww.jav  a2  s .  co  m
import android.text.TextUtils;

import com.orm.dsl.Column;
import com.orm.dsl.Table;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class NamingHelper {

    /**
     * Converts a given CamelCasedString to UPPER_CASE_UNDER_SCORE.
     *
     * @param camelCased  a non empty camelCased string
     * @return the equivalent string converted to UPPER_CASE_UNDER_SCORE unless camelCased equals
     *         "_id" (not case sensitive) in which case "_id" is returned
     */
    public static String toSQLNameDefault(String camelCased) {
        if (camelCased.equalsIgnoreCase("_id")) {
            return "_id";
        }

        StringBuilder sb = new StringBuilder();
        char[] buf = camelCased.toCharArray();

        for (int i = 0; i < buf.length; i++) {
            char prevChar = (i > 0) ? buf[i - 1] : 0;
            char c = buf[i];
            char nextChar = (i < buf.length - 1) ? buf[i + 1] : 0;
            boolean isFirstChar = (i == 0);

            if (isFirstChar || Character.isLowerCase(c) || Character.isDigit(c)) {
                sb.append(Character.toUpperCase(c));
            } else if (Character.isUpperCase(c)) {
                if (Character.isLetterOrDigit(prevChar)) {
                    if (Character.isLowerCase(prevChar)) {
                        sb.append('_').append(c);
                    } else if (nextChar > 0 && Character.isLowerCase(nextChar)) {
                        sb.append('_').append(c);
                    } else {
                        sb.append(c);
                    }
                } else {
                    sb.append(c);
                }
            }
        }

        return sb.toString();
    }

    /**
     * Maps a Java Field object to the database's column name.
     *
     * @param field  the {@link java.lang.reflect.Field} that will be mapped
     * @return the name of the given Field as represented in the database. If the Field is annotated
     *         with {@link com.orm.dsl.Column} then the {@link com.orm.dsl.Column#name()} will be
     *         returned. Else, the Field's {@link java.lang.reflect.Field#getName()} will be
     *         converted from CamelCase to UNDER_SCORE notation
     */
    public static String toSQLName(Field field) {
        if (field.isAnnotationPresent(Column.class)) {
            Column annotation = field.getAnnotation(Column.class);
            return annotation.name();
        }

        return toSQLNameDefault(field.getName());
    }

    /**
     * Maps a Java Class to the name of the class.
     *
     * @param table  the generic {@link java.lang.Class<T>} that defines a database table
     * @return if the given class is annotated with {@link com.orm.dsl.Table} then the value for
     *         {@link com.orm.dsl.Table#name()} will be returned. Else, the class' simple name will 
     *         be converted from CamelCase to UNDER_SCORE notation
     */
    public static String toSQLName(Class<?> table) {
        if (table.isAnnotationPresent(Table.class)) {
            Table annotation = table.getAnnotation(Table.class);
            if ("".equals(annotation.name())) {
                return NamingHelper.toSQLNameDefault(table.getSimpleName());
            }
            return annotation.name();
        }

        return NamingHelper.toSQLNameDefault(table.getSimpleName());
    }

}




Java Source Code List

com.example.AddNoteActivity.java
com.example.ClientApp.java
com.example.NewNote.java
com.example.NoteListActivity.java
com.example.NoteRelation.java
com.example.Note.java
com.example.SugarActivity.java
com.example.Tag.java
com.example.TextNote.java
com.orm.SchemaGenerator.java
com.orm.SugarApp.java
com.orm.SugarContext.java
com.orm.SugarDb.java
com.orm.SugarRecord.java
com.orm.SugarTransactionHelper.java
com.orm.dsl.Column.java
com.orm.dsl.Ignore.java
com.orm.dsl.NotNull.java
com.orm.dsl.Table.java
com.orm.dsl.Unique.java
com.orm.query.Condition.java
com.orm.query.Select.java
com.orm.util.Collection.java
com.orm.util.ManifestHelper.java
com.orm.util.NamingHelper.java
com.orm.util.NumberComparator.java
com.orm.util.QueryBuilder.java
com.orm.util.ReflectionUtil.java
com.orm.util.SugarConfig.java
com.orm.util.SugarCursorFactory.java