summaryrefslogtreecommitdiff
path: root/qpid/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/codec/ListWriter.java
blob: 3e0164705fa8d0727b0ec1328210a47e1c6527de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.qpid.amqp_1_0.codec;

import java.nio.ByteBuffer;
import java.util.List;

public class ListWriter implements ValueWriter<List>
{
    private static class NonEmptyListWriter extends AbstractListWriter<List>
    {
        private List _list;
        private int _position = 0;

        public NonEmptyListWriter(final Registry registry)
        {
            super(registry);
        }

        @Override
        protected void onSetValue(final List value)
        {
            _list = value;
            _position = 0;

        }

        @Override
        protected int getCount()
        {
            return _list.size();
        }

        @Override
        protected boolean hasNext()
        {
            return _position < getCount();
        }

        @Override
        protected Object next()
        {
            return _list.get(_position++);
        }

        @Override
        protected void clear()
        {
            _list = null;
            _position = 0;
        }

        @Override
        protected void reset()
        {
            _position = 0;
        }

    }

    private final NonEmptyListWriter _nonEmptyListWriter;
    private static final byte ZERO_BYTE_FORMAT_CODE = (byte) 0x45;

    private final ValueWriter<List> _emptyListWriter = new EmptyListValueWriter();


    private ValueWriter<List> _delegate;

    public ListWriter(final Registry registry)
    {
        _nonEmptyListWriter = new NonEmptyListWriter(registry);

    }


    public int writeToBuffer(ByteBuffer buffer)
    {
        return _delegate.writeToBuffer(buffer);
    }

    public void setValue(List frameBody)
    {
        if(frameBody.isEmpty())
        {
            _delegate = _emptyListWriter;
        }
        else
        {
            _delegate = _nonEmptyListWriter;
        }
        _delegate.setValue(frameBody);
    }

    public boolean isComplete()
    {
        return _delegate.isComplete();
    }

    public boolean isCacheable()
    {
        return false;
    }



    private static Factory<List> FACTORY = new Factory<List>()
                                            {

                                                public ValueWriter<List> newInstance(Registry registry)
                                                {
                                                    return new ListWriter(registry);
                                                }
                                            };

    public static void register(ValueWriter.Registry registry)
    {
        registry.register(List.class, FACTORY);
    }

    public static class EmptyListValueWriter implements ValueWriter<List>
    {
        private boolean _complete;


        public int writeToBuffer(ByteBuffer buffer)
        {

            if(!_complete && buffer.hasRemaining())
            {
                buffer.put(ZERO_BYTE_FORMAT_CODE);
                _complete = true;
            }

            return 1;
        }

        public void setValue(List list)
        {
            _complete = false;
        }

        public boolean isCacheable()
        {
            return true;
        }

        public boolean isComplete()
        {
            return _complete;
        }

    }
}