Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.datastax.driver.mapping;
  
  import java.util.List;
  
A CQL type definition parsed from com.datastax.driver.mapping.annotations.Frozen.value(), for example map<text,map<text,frozen<user>>>
  
 class CQLType {
     String name;
     boolean frozen;
     List<CQLTypesubTypes;
 
     private CQLType(String name) {
         this(namefalse);
     }
 
     private CQLType(String nameboolean frozenCQLType... subTypes) {
         this. = name;
         this. = frozen;
         for (CQLType subType : subTypes) {
             if (this. == null)
                 this. = new ArrayList<CQLType>();
             this..add(subType);
         }
     }
 
     // Some constant types for the simple cases. Note that we can afford generic names because currently we don't include the name in our
     // checks.
     static final CQLType FROZEN_SIMPLE = new CQLType("root"true);
     static final CQLType FROZEN_ELEMENT = new CQLType("collection"false,
                                                       new CQLType("element"true));
     static final CQLType FROZEN_MAP_KEY = new CQLType("map"false,
                                                       new CQLType("key"true));
     static final CQLType FROZEN_MAP_VALUE = new CQLType("map"false,
                                                         new CQLType("key"false),
                                                         new CQLType("value"true));
     static final CQLType FROZEN_MAP_KEY_AND_VALUE = new CQLType("map"false,
                                                                 new CQLType("key"true),
                                                                 new CQLType("value"true));
     static final CQLType UNFROZEN_SIMPLE = new CQLType("root");
 
     static CQLType parse(String toParse) {
         if (Strings.isNullOrEmpty(toParse))
             // we allow that as a shorthand
             return ;
         else
             return new Parser(toParse).parse();
     }
 
     private static class Parser {
         private final String toParse;
         private int idx;
 
         Parser(String toParse) {
             this. = toParse;
         }
 
         CQLType parse() {
             skipSpaces();
 
             if (.charAt() == '"') {
                 // quoted identifier: just return everything up to the next quote
                 int endQuote =  + 1;
                 while (endQuote < .length() && .charAt(endQuote) != '"')
                     endQuote += 1;
                 if (endQuote == .length())
                     throw fail("could not find matching quote");
                 if (endQuote ==  + 1)
                     throw fail("empty quoted identifier");
 
                 String name = .substring( + 1, endQuote);
                  = endQuote + 1;
                 // a quoted identifier cannot be parameterized, so we can return immediately
                 return new CQLType(name);
             } else {
                 // unquoted identifier: could be "simpleType", "complexType<subType1, subType2...>" or "frozen<type>"
                 int n = skipWord();
                 String nextWord = .substring(n).toLowerCase();
                  = n;
                 if ("frozen".equals(nextWord)) {
                     skipSpaces();
                     if ( >= .length() || .charAt() != '<')
                         throw fail("expected '<'");
                      += 1;
                     CQLType type = parse();
                     skipSpaces();
                     if ( >= .length() || .charAt() != '>')
                         throw fail("expected '>'");
                      += 1;
                     type.frozen = true;
                     return type;
                 } else {
                     CQLType type = new CQLType(nextWord);
                     skipSpaces();
                     if ( < .length() && .charAt() == '<') {
                          += 1;
                         type.subTypes = new ArrayList<CQLType>();
                        while ( < .length()) {
                            type.subTypes.add(parse());
                            skipSpaces();
                            if ( >= .length())
                                fail("unterminated list of subtypes");
                            else if (.charAt() == '>') {
                                 += 1;
                                break;
                            }
                            else if (.charAt() != ',')
                                fail("expected ','");
                            else
                                 += 1;
                        }
                    }
                    return type;
                }
            }
        }
        private void skipSpaces() {
            while ( < .length() && isBlank())
                 += 1;
        }
        private int skipWord() {
            if ( >= .length())
                throw fail("expected type name");
            if (!isIdentStart())
                throw fail("illegal character at start of type name");
            int i = ;
            while (i < .length() && isIdentBody(i))
                i += 1;
            return i;
        }
        private boolean isBlank(int i) {
            char c = .charAt(i);
            return c == ' ' || c == '\t' || c == '\n';
        }
        private boolean isIdentStart(int i) {
            char c = .charAt(i);
            return isLetter(c);
        }
        private boolean isIdentBody(int i) {
            char c = .charAt(i);
            return isLetter(c) || isDigit(c) || c == '_';
        }
        private boolean isLetter(char c) {
            return (c >= 'a' && c <= 'z') ||
                   (c >= 'A' && c <= 'Z');
        }
        private boolean isDigit(char c) {
            return (c >= '0' && c <= '9');
        }
        private IllegalArgumentException fail(String cause) {
            return new IllegalArgumentException(cause + " (" +  + " [" +  + "])");
        }
    }
New to GrepCode? Check out our FAQ X