Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d4b0645a3b4d1e304dad1ddd3d4c29c77046c4be (plain) (blame)
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
173
174
175
176
177
/*
 * Copyright (c) 2008-2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Simon McDuff  - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.internal.cdo.query;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.util.CDOQueryInfo;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.ObjectNotFoundException;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.spi.cdo.AbstractQueryIterator;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Simon McDuff
 */
public class CDOQueryResultIteratorImpl<T> extends AbstractQueryIterator<T>
{
  private Map<CDOID, CDOObject> detachedObjects;

  public CDOQueryResultIteratorImpl(CDOView view, CDOQueryInfo queryInfo)
  {
    super(view, queryInfo);
  }

  @Override
  public void close()
  {
    detachedObjects = null;
    super.close();
  }

  @Override
  public T next()
  {
    return adapt(super.next());
  }

  @SuppressWarnings("unchecked")
  protected T adapt(Object object)
  {
    if (object instanceof CDOID)
    {
      CDOID id = (CDOID)object;
      if (id.isNull())
      {
        return null;
      }

      CDOView view = getView();

      try
      {
        CDOObject cdoObject = view.getObject(id, true);
        return (T)CDOUtil.getEObject(cdoObject);
      }
      catch (ObjectNotFoundException ex)
      {
        if (view instanceof CDOTransaction)
        {
          if (detachedObjects == null)
          {
            CDOTransaction transaction = (CDOTransaction)view;
            detachedObjects = transaction.getDetachedObjects();
          }

          CDOObject cdoObject = detachedObjects.get(id);
          return (T)CDOUtil.getEObject(cdoObject);
        }

        return null;
      }
    }

    // Support a query return value of Object[]
    if (object instanceof Object[])
    {
      Object[] objects = (Object[])object;
      Object[] resolvedObjects = new Object[objects.length];
      for (int i = 0; i < objects.length; i++)
      {
        if (objects[i] instanceof CDOID)
        {
          resolvedObjects[i] = adapt(objects[i]);
        }
        else
        {
          resolvedObjects[i] = objects[i];
        }
      }

      return (T)resolvedObjects;
    }

    return (T)object;
  }

  @Override
  public List<T> asList()
  {
    List<Object> result = new ArrayList<Object>();
    while (super.hasNext())
    {
      result.add(super.next());
    }

    return new QueryResultList(result);
  }

  @Override
  public T asValue()
  {
    if (hasNext())
    {
      return next();
    }

    return null;
  }

  /**
   * @author Simon McDuff
   */
  private class QueryResultList extends AbstractList<T>implements EList<T>
  {
    private List<Object> objects;

    public QueryResultList(List<Object> objects)
    {
      this.objects = objects;
    }

    @Override
    public T get(int index)
    {
      return adapt(objects.get(index));
    }

    @Override
    public int size()
    {
      return objects.size();
    }

    @Override
    public String toString()
    {
      return "QueryResultList" + objects.toString();
    }

    public void move(int newPosition, T object)
    {
      throw new UnsupportedOperationException();
    }

    public T move(int newPosition, int oldPosition)
    {
      throw new UnsupportedOperationException();
    }
  }
}

Back to the top