001 /*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 *
020 */
021 package org.apache.qpid.transport;
022
023 import java.util.List;
024 import java.util.Map;
025
026 import org.apache.qpid.transport.codec.Decoder;
027 import org.apache.qpid.transport.codec.Encodable;
028 import org.apache.qpid.transport.codec.Encoder;
029
030
031 /**
032 * Struct
033 *
034 * @author Rafael H. Schloming
035 */
036
037 public abstract class Struct implements Encodable
038 {
039
040 public static Struct create(int type)
041 {
042 return StructFactory.create(type);
043 }
044
045 boolean dirty = true;
046
047 public boolean isDirty()
048 {
049 return dirty;
050 }
051
052 public void setDirty(boolean dirty)
053 {
054 this.dirty = dirty;
055 }
056
057 public abstract int getStructType();
058
059 public abstract int getSizeWidth();
060
061 public abstract int getPackWidth();
062
063 public final int getEncodedType()
064 {
065 int type = getStructType();
066 if (type < 0)
067 {
068 throw new UnsupportedOperationException();
069 }
070 return type;
071 }
072
073 private final boolean isBit(Field<?,?> f)
074 {
075 return f.getType().equals(Boolean.class);
076 }
077
078 private final boolean packed()
079 {
080 return getPackWidth() > 0;
081 }
082
083 private final boolean encoded(Field<?,?> f)
084 {
085 return !packed() || !isBit(f) && f.has(this);
086 }
087
088 private final int getFlagWidth()
089 {
090 return (getFields().size() + 7)/8;
091 }
092
093 private final int getPaddWidth()
094 {
095 int pw = getPackWidth() - getFlagWidth();
096 assert pw > 0;
097 return pw;
098 }
099
100 private final int getFlagCount()
101 {
102 return 8*getPackWidth();
103 }
104
105 private final int getReservedFlagCount()
106 {
107 return getFlagCount() - getFields().size();
108 }
109
110 public abstract void read(Decoder dec);
111
112 public abstract void write(Encoder enc);
113
114 public abstract Map<String,Object> getFields();
115
116 public String toString()
117 {
118 StringBuilder str = new StringBuilder();
119 str.append(getClass().getSimpleName());
120
121 str.append("(");
122 boolean first = true;
123 for (Map.Entry<String,Object> me : getFields().entrySet())
124 {
125 if (first)
126 {
127 first = false;
128 }
129 else
130 {
131 str.append(", ");
132 }
133 str.append(me.getKey());
134 str.append("=");
135 str.append(me.getValue());
136 }
137 str.append(")");
138
139 return str.toString();
140 }
141
142 }
|