Android Open Source - spades Mapped Field Factory






From Project

Back to project page spades.

License

The source code is released under:

Apache License

If you think the Android project spades 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) 2013 Pau Picas Sans <pau.picas@gmail.com>
 */*from  w ww.  j av a 2  s  .co m*/
 * 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 cat.picas.spades.map;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;
import cat.picas.spades.ColumnBuilder.ColumnType;
import cat.picas.spades.RelatedParent;

public class MappedFieldFactory {

  private static volatile MappedFieldFactory sInstance;

  public static MappedFieldFactory getInstance() {
    if (sInstance == null) {
      synchronized (MappedFieldFactory.class) {
        if (sInstance == null) {
          sInstance = new MappedFieldFactory();
          registerCoreValueMappers(sInstance);
        }
      }
    }

    return sInstance;
  }

  private static void registerCoreValueMappers(MappedFieldFactory factory) {
    IntegerMapper integerMapper = new IntegerMapper();
    factory.registerValueMapper(Integer.TYPE, integerMapper, ColumnType.INTEGER);
    factory.registerValueMapper(Integer.class, integerMapper, ColumnType.INTEGER);

    LongMapper longMapper = new LongMapper();
    factory.registerValueMapper(Long.TYPE, longMapper, ColumnType.INTEGER);
    factory.registerValueMapper(Long.class, longMapper, ColumnType.INTEGER);

    DoubleMapper doubleMapper = new DoubleMapper();
    factory.registerValueMapper(Double.TYPE, doubleMapper, ColumnType.REAL);
    factory.registerValueMapper(Double.class, doubleMapper, ColumnType.REAL);

    BooleanMapper booleanMapper = new BooleanMapper();
    factory.registerValueMapper(Boolean.TYPE, booleanMapper, ColumnType.INTEGER);
    factory.registerValueMapper(Boolean.class, booleanMapper, ColumnType.INTEGER);

    factory.registerValueMapper(String.class, new StringMapper(), ColumnType.TEXT);

    factory.registerValueMapper(Date.class, new DateMapper(), ColumnType.INTEGER);

    factory.registerValueMapper(RelatedParent.class, new RelatedParentMapper(), ColumnType.INTEGER);

    factory.registerValueMapper(Enum.class, new EnumMapper(), ColumnType.TEXT);
  }

  private Map<Class<?>, ValueMapper> mValueMappers = new HashMap<Class<?>, ValueMapper>();
  private Map<Class<?>, ColumnType> mColumnTypes = new HashMap<Class<?>, ColumnType>();

  private MappedFieldFactory() {
  }

  public void registerValueMapper(Class<?> cls, ValueMapper valueMapper, ColumnType columnType) {
    mValueMappers.put(cls, valueMapper);
    mColumnTypes.put(cls, columnType);
  }

  public MappedField createForField(Field field) {
    ValueMapper valueMapper = mValueMappers.get(field.getType());
    ColumnType columnType = mColumnTypes.get(field.getType());

    if (valueMapper == null) {
      for (Class<?> cls : mValueMappers.keySet()) {
        if (cls.isAssignableFrom(field.getType())) {
          valueMapper = mValueMappers.get(cls);
          columnType = mColumnTypes.get(cls);
          break;
        }
      }
    }

    if (valueMapper == null) {
      throw new IllegalArgumentException("The type of the field '" + field.getName() + "' "
          + " is not supported: " + field.getType().getName());
    }

    return new MappedFieldImpl(field, valueMapper, columnType);
  }

  private static class MappedFieldImpl implements MappedField {
    private Field mField;
    private ValueMapper mMapper;
    private ColumnType mColumnType;

    private MappedFieldImpl(Field field, ValueMapper mapper, ColumnType columnType) {
      mField = field;
      mMapper = mapper;
      mColumnType = columnType;

      mField.setAccessible(true);
    }

    @Override
    public void putContetValue(Object object, ContentValues values, String key, boolean notNull) {
      mMapper.putContetValue(mField, object, values, key, notNull);
    }

    @Override
    public void setFieldValue(Object object, Cursor cursor, int index) {
      mMapper.setFieldValue(mField, object, cursor, index);
    }

    @Override
    public Field getField() {
      return mField;
    }

    @Override
    public ColumnType getColumnType() {
      return mColumnType;
    }
  }

}




Java Source Code List

cat.picas.spades.AbstractEntity.java
cat.picas.spades.AutoEntityMapper.java
cat.picas.spades.ColumnBuilder.java
cat.picas.spades.Column.java
cat.picas.spades.CursorInfoBuilder.java
cat.picas.spades.CursorInfo.java
cat.picas.spades.Dao.java
cat.picas.spades.EntityMapper.java
cat.picas.spades.Entity.java
cat.picas.spades.RelatedChild.java
cat.picas.spades.RelatedList.java
cat.picas.spades.RelatedParent.java
cat.picas.spades.SqlHelper.java
cat.picas.spades.Table.java
cat.picas.spades.Tables.java
cat.picas.spades.fetch.ArrayListFetchStrategy.java
cat.picas.spades.fetch.FetchStrategy.java
cat.picas.spades.fetch.HashMapFetchStrategy.java
cat.picas.spades.map.BooleanMapper.java
cat.picas.spades.map.DateMapper.java
cat.picas.spades.map.DoubleMapper.java
cat.picas.spades.map.EnumMapper.java
cat.picas.spades.map.IntegerMapper.java
cat.picas.spades.map.LongMapper.java
cat.picas.spades.map.MappedFieldFactory.java
cat.picas.spades.map.MappedField.java
cat.picas.spades.map.RelatedParentMapper.java
cat.picas.spades.map.StringMapper.java
cat.picas.spades.map.ValueMapper.java
cat.picas.spades.query.ColumnSelector.java
cat.picas.spades.query.NameMapper.java
cat.picas.spades.query.Query.java
cat.picas.spades.query.SelectedColumn.java
cat.picas.spades.util.ReflectionUtils.java
cat.picas.spadessamples.AliasTableActivity.java
cat.picas.spadessamples.ArrayListActivity.java
cat.picas.spadessamples.CursorListActivity.java
cat.picas.spadessamples.MainActivity.java
cat.picas.spadessamples.PersonDetailActivity.java
cat.picas.spadessamples.adapter.CouplesCursorAdapter.java
cat.picas.spadessamples.adapter.PersonArrayAdapter.java
cat.picas.spadessamples.adapter.PersonCursorAdapter.java
cat.picas.spadessamples.model.ContactPointDao.java
cat.picas.spadessamples.model.ContactPoint.java
cat.picas.spadessamples.model.DatabaseHelper.java
cat.picas.spadessamples.model.FixturesHelper.java
cat.picas.spadessamples.model.PersonDao.java
cat.picas.spadessamples.model.Person.java
cat.picas.spadessamples.model.inheritance.HotelDao.java
cat.picas.spadessamples.model.inheritance.Hotel.java
cat.picas.spadessamples.model.inheritance.PlaceDao.java
cat.picas.spadessamples.model.inheritance.Place.java
cat.picas.spadessamples.model.inheritance.RestaurantDao.java
cat.picas.spadessamples.model.inheritance.Restaurant.java