Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package test;
  
  import java.util.*;
  
Value class for performance tests
 
 public class MediaItem
 {
     private List<Photo_photos;
     private Content _content;
 
     public MediaItem() { }
 
     public MediaItem(Content c)
     {
          = c;
     }
 
     public void addPhoto(Photo p) {
         if ( == null) {
              = new ArrayList<Photo>();
         }
         .add(p);
     }
     
     public List<PhotogetImages() { return ; }
     public void setImages(List<Photop) {  = p; }
 
     public Content getContent() { return ; }
     public void setContent(Content c) {  = c; }
 
     // Custom deser
     public static MediaItem deserialize(JsonParser jpthrows IOException
     {
         if (jp.nextToken() != .) {
             throw new IOException("Need START_OBJECT for MediaItem");
         }
         MediaItem item = new MediaItem();
         while (jp.nextToken() == .) {
             String name = jp.getCurrentName();
             if (name == "images") {
                 item._photos = deserializeImages(jp);
             } else if (name == "content") {
                 item._content = Content.deserialize(jp);
             } else throw new IOException("Unknown field");
         }
         if (jp.getCurrentToken() != .) {
             throw new IOException("Need END_OBJECT to complete MediaItem");
         }
         return item;
     }
 
     private static List<PhotodeserializeImages(JsonParser jpthrows IOException
     {
         if (jp.nextToken() != .) {
             throw new IOException("Need START_ARRAY for List of Photos");
         }
         ArrayList<Photoimages = new ArrayList<Photo>(4);
         while (jp.nextToken() == .) {
             images.add(Photo.deserialize(jp));
         }
         if (jp.getCurrentToken() != .) {
             throw new IOException("Need END_ARRAY to complete List of Photos");
         }
         return images;
     }
     
     // Custom serializer
     public void serialize(JsonGenerator jgenthrows IOException
     {
         jgen.writeStartObject();
 
         if ( == null) {
             jgen.writeNullField("images");
         } else {
             jgen.writeArrayFieldStart("images");
             for (Photo photo : ) {
                 photo.serialize(jgen);
             }
             jgen.writeEndArray();
         }
         jgen.writeFieldName("content");
         if ( == null) {
             jgen.writeNull();
         } else {
             .serialize(jgen);
         }
 
         jgen.writeEndObject();
     }
     
     /*
     /**********************************************************
     /* Helper types
     /**********************************************************
     */
    
    public static class Photo
    {
        public final static int F_URI = 1;
        public final static int F_TITLE = 2;
        public final static int F_WIDTH = 3;
        public final static int F_HEIGHT = 4;
        public final static int F_SIZE = 5;
        
        public final static HashMap<String,IntegersFields = new HashMap<String,Integer>();
        static {
            // MediaItem fields
            .put("uri");
            .put("title");
            .put("width");
            .put("height");
            .put("size");
        }
        
        public enum Size { SMALL, LARGE; }
    
      private String _uri;
      private String _title;
      private int _width;
      private int _height;
      private Size _size;
    
      public Photo() {}
      public Photo(String uriString titleint wint hSize s)
      {
           = uri;
           = title;
           = w;
           = h;
           = s;
      }
    
      public String getUri() { return ; }
      public String getTitle() { return ; }
      public int getWidth() { return ; }
      public int getHeight() { return ; }
      public Size getSize() { return ; }
    
      public void setUri(String u) {  = u; }
      public void setTitle(String t) {  = t; }
      public void setWidth(int w) {  = w; }
      public void setHeight(int h) {  = h; }
      public void setSize(Size s) {  = s; }
      private static Size findSize(String id)
      {
          if (id.charAt(0) == 'L') {
              if ("LARGE".equals(id)) {
                  return .;
              }
          } else if ("SMALL".equals(id)) {
              return .;
          }
          throw new IllegalArgumentException();
      }
      
      public static Photo deserialize(JsonParser jpthrows IOException
      {
          Photo photo = new Photo();
          while (jp.nextToken() == .) {
              String name = jp.getCurrentName();
              jp.nextToken();
              Integer I = .get(name);
              if (I != null) {
                  switch (I.intValue()) {
                  case :
                      photo.setUri(jp.getText());
                      continue;
                  case :
                      photo.setTitle(jp.getText());
                      continue;
                  case :
                      photo.setWidth(jp.getIntValue());
                      continue;
                  case :
                      photo.setHeight(jp.getIntValue());
                      continue;
                  case :
                      photo.setSize(findSize(jp.getText()));
                      continue;
                  }
              }
              throw new IOException("Unknown field '"+name+"'");
          }
          if (jp.getCurrentToken() != .) {
              throw new IOException("Need END_OBJECT to complete Photo");
          }
          return photo;
      }
      
      public void serialize(JsonGenerator jgenthrows IOException
      {
          jgen.writeStartObject();
          jgen.writeStringField("uri");
          jgen.writeStringField("title");
          jgen.writeNumberField("width");
          jgen.writeNumberField("height");
          jgen.writeStringField("size", ( == null) ? null : .name());
          jgen.writeEndObject();
      }          
    }
    public static class Content
    {
        public final static int F_PLAYER = 0;
        public final static int F_URI = 1;
        public final static int F_TITLE = 2;
        public final static int F_WIDTH = 3;
        public final static int F_HEIGHT = 4;
        public final static int F_FORMAT = 5;
        public final static int F_DURATION = 6;
        public final static int F_SIZE = 7;
        public final static int F_BITRATE = 8;
        public final static int F_PERSONS = 9;
        public final static int F_COPYRIGHT = 10;
        
        public final static HashMap<String,IntegersFields = new HashMap<String,Integer>();
        static {
            .put("player");
            .put("uri");
            .put("title");
            .put("width");
            .put("height");
            .put("format");
            .put("duration");
            .put("size");
            .put("bitrate");
            .put("persons");
            .put("copyright");
        }
        public enum Player { JAVA, FLASH;  }
    
        private Player _player;
        private String _uri;
        private String _title;
        private int _width;
        private int _height;
        private String _format;
        private long _duration;
        private long _size;
        private int _bitrate;
        private List<String_persons;
        private String _copyright;
    
        public Content() { }
        public void addPerson(String p) {
            if ( == null) {
                 = new ArrayList<String>();
            }
            .add(p);
        }
        
        public Player getPlayer() { return ; }
        public String getUri() { return ; }
        public String getTitle() { return ; }
        public int getWidth() { return ; }
        public int getHeight() { return ; }
        public String getFormat() { return ; }
        public long getDuration() { return ; }
        public long getSize() { return ; }
        public int getBitrate() { return ; }
        public List<StringgetPersons() { return ; }
        public String getCopyright() { return ; }
    
        public void setPlayer(Player p) {  = p; }
        public void setUri(String u) {   = u; }
        public void setTitle(String t) {   = t; }
        public void setWidth(int w) {   = w; }
        public void setHeight(int h) {   = h; }
        public void setFormat(String f) {   = f;  }
        public void setDuration(long d) {   = d; }
        public void setSize(long s) {   = s; }
        public void setBitrate(int b) {   = b; }
        public void setPersons(List<Stringp) {   = p; }
        public void setCopyright(String c) {   = c; }
        private static Player findPlayer(String id)
        {
            if ("JAVA".equals(id)) {
                return .;
            }
            if ("FLASH".equals(id)) {
                return .;
            }
            throw new IllegalArgumentException();
        }
        
        public static Content deserialize(JsonParser jpthrows IOException
        {
            if (jp.nextToken() != .) {
                throw new IOException("Need START_OBJECT for Content");
            }
            Content content = new Content();
            while (jp.nextToken() == .) {
                String name = jp.getCurrentName();
                jp.nextToken();
                Integer I = .get(name);
                if (I != null) {
                    switch (I.intValue()) {
                    case :
                        content.setPlayer(findPlayer(jp.getText()));
                    case :
                        content.setUri(jp.getText());
                        continue;
                    case :
                        content.setTitle(jp.getText());
                        continue;
                    case :
                        content.setWidth(jp.getIntValue());
                        continue;
                    case :
                        content.setHeight(jp.getIntValue());
                        continue;
                    case :
                        content.setCopyright(jp.getText());
                        continue;
                    case :
                        content.setDuration(jp.getLongValue());
                        continue;
                    case :
                        content.setSize(jp.getLongValue());
                        continue;
                    case :
                        content.setBitrate(jp.getIntValue());
                        continue;
                    case :
                        content.setPersons(deserializePersons(jp));
                        continue;
                    case :
                        content.setCopyright(jp.getText());
                        continue;
                    }
                }
                throw new IOException("Unknown field '"+name+"'");
            }
            if (jp.getCurrentToken() != .) {
                throw new IOException("Need END_OBJECT to complete Content");
            }
            return content;
        }
        private static List<StringdeserializePersons(JsonParser jpthrows IOException
        {
            if (jp.getCurrentToken() != .) {
                throw new IOException("Need START_ARRAY for List of Persons (got "+jp.getCurrentToken()+")");
            }
            ArrayList<Stringpersons = new ArrayList<String>(4);
            while (jp.nextToken() == .) {
                persons.add(jp.getText());
            }
            if (jp.getCurrentToken() != .) {
                throw new IOException("Need END_ARRAY to complete List of Persons");
            }
            return persons;
        }
        
        public void serialize(JsonGenerator jgenthrows IOException
        {
            jgen.writeStartObject();
            jgen.writeStringField("play", ( == null) ? null : .name());
            jgen.writeStringField("uri");
            jgen.writeStringField("title");
            jgen.writeNumberField("width");
            jgen.writeNumberField("height");
            jgen.writeStringField("format");
            jgen.writeNumberField("duration");
            jgen.writeNumberField("size");
            jgen.writeNumberField("bitrate");
            jgen.writeStringField("copyright");
            if ( == null) {
                jgen.writeNullField("persons");
            } else {
                jgen.writeArrayFieldStart("persons");
                for (String p : ) {
                    jgen.writeString(p);
                }
                jgen.writeEndArray();
            }
            jgen.writeEndObject();
        }          
    }
New to GrepCode? Check out our FAQ X