Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *      Copyright (C) 2012-2014 DataStax Inc.
   *
   *   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 com.datastax.driver.mapping;
 
 import java.util.Map;
 
 
 
Mapping manager from which to obtain entity mappers.
 
 public class MappingManager {
 
     private final Session session;
 
     private volatile Map<Class<?>, Mapper<?>> mappers = Collections.<Class<?>, Mapper<?>>emptyMap();
     private volatile Map<Class<?>, UDTMapper<?>> udtMappers = Collections.<Class<?>, UDTMapper<?>>emptyMap();
     private volatile Map<Class<?>, Objectaccessors = Collections.<Class<?>, Object>emptyMap();

    
Creates a new MappingManager using the provided Session.

Parameters:
session the Session to use.
 
     public MappingManager(Session session) {
         this. = session;
     }

    
The underlying Session used by this manager.

Note that you can get obtain the Cluster object corresponding to that session using getSession().getCluster().

It is inadvisable to close the returned Session while this manager and its mappers are in use.

Returns:
the underlying session used by this manager.
 
     public Session getSession() {
         return ;
     }

    
Creates a Mapper for the provided class (that must be annotated by a com.datastax.driver.mapping.annotations.Table annotation).

The MappingManager only ever keeps one Mapper for each class, and so calling this method multiple times on the same class will always return the same object.

Parameters:
<T> the type of the class to map.
klass the (annotated) class for which to return the mapper.
Returns:
the Mapper object for class klass.
 
     public <T> Mapper<T> mapper(Class<T> klass) {
         return getMapper(klass);
     }

    
Creates a UDTMapper for the provided class (that must be annotated by a com.datastax.driver.mapping.annotations.UDT annotation).

The MappingManager only ever keeps one UDTMapper for each class, and so calling this method multiple times on the same class will always return the same object.

Parameters:
<T> the type of the class to map.
klass the (annotated) class for which to return the mapper.
Returns:
the UDTMapper object for class klass.
 
     public <T> UDTMapper<T> udtMapper(Class<T> klass) {
         return getUDTMapper(klass);
     }

    
Creates an accessor object based on the provided interface (that must be annotated by a com.datastax.driver.mapping.annotations.Accessor annotation).

The MappingManager only ever keep one Accessor for each class, and so calling this method multiple time on the same class will always return the same object.

Parameters:
<T> the type of the accessor class.
klass the (annotated) class for which to create an accessor object.
Returns:
the accessor object for class klass.
    public <T> T createAccessor(Class<T> klass) {
        return getAccessor(klass);
    }
    @SuppressWarnings("unchecked")
    private <T> Mapper<T> getMapper(Class<T> klass) {
        Mapper<T> mapper = (Mapper<T>).get(klass);
        if (mapper == null) {
            synchronized () {
                mapper = (Mapper<T>).get(klass);
                if (mapper == null) {
                    EntityMapper<T> entityMapper = AnnotationParser.parseEntity(klass, ReflectionMapper.factory(), this);
                    mapper = new Mapper<T>(thisklassentityMapper);
                    Map<Class<?>, Mapper<?>> newMappers = new HashMap<Class<?>, Mapper<?>>();
                    newMappers.put(klassmapper);
                     = newMappers;
                }
            }
        }
        return mapper;
    }
    @SuppressWarnings("unchecked")
    <T> UDTMapper<T> getUDTMapper(Class<T> klass) {
        UDTMapper<T> mapper = (UDTMapper<T>).get(klass);
        if (mapper == null) {
            synchronized () {
                mapper = (UDTMapper<T>).get(klass);
                if (mapper == null) {
                    EntityMapper<T> entityMapper = AnnotationParser.parseUDT(klass, ReflectionMapper.factory(), this);
                    mapper = new UDTMapper<T>(entityMapper);
                    Map<Class<?>, UDTMapper<?>> newMappers = new HashMap<Class<?>, UDTMapper<?>>();
                    newMappers.put(klassmapper);
                     = newMappers;
                }
            }
        }
        return mapper;
    }
    @SuppressWarnings("unchecked")
    private <T> T getAccessor(Class<T> klass) {
        T accessor = (T).get(klass);
        if (accessor == null) {
            synchronized () {
                accessor = (T).get(klass);
                if (accessor == null) {
                    AccessorMapper<T> mapper = AnnotationParser.parseAccessor(klass, AccessorReflectionMapper.factory(), this);
                    mapper.prepare(this);
                    accessor = mapper.createProxy();
                    Map<Class<?>, ObjectnewAccessors = new HashMap<Class<?>, Object>();
                    newAccessors.put(klassaccessor);
                     = newAccessors;
                }
            }
        }
        return accessor;
    }
New to GrepCode? Check out our FAQ X