summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-1.1-incubating/impl/src/main/java/org/apache/tuscany/sdo/util/VirtualSequence.java
blob: a5982ceb35cce917f460a8d5ab07c7728fffc277 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/**
 *
 *  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.tuscany.sdo.util;

import java.util.Iterator;
import java.util.List;

import org.apache.tuscany.sdo.impl.ClassImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.FeatureMapUtil;

import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Sequence;

/**
 *  SDO Sequance implementation which delegates to a Collection of DataObject properties.
 */
public class VirtualSequence implements Sequence
{
  private final DataObject dataObject;
  protected final List delegateProperties; // size > 1

  public VirtualSequence(DataObject object)
  {
    dataObject = object;
    delegateProperties = ((ClassImpl)dataObject.getType()).getVirtualSequenceProperties();
  }
  
  static protected boolean isSequenceProperty(Object property)
  {
    return FeatureMapUtil.isFeatureMap((EStructuralFeature)property);
  }
  
  public int size()
  {
    int size = 0;
    for (Iterator iterator = delegateProperties.iterator() ; iterator.hasNext() ;)
    {
      Property property = (Property)iterator.next();
      if (dataObject.isSet(property))
        if (isSequenceProperty(property))
          size += ((Sequence)dataObject.get(property)).size();
        else if (property.isMany())
          size += dataObject.getList(property).size();
        else
          ++size;
    }
    return size;
  }

  public Property getProperty(int index)
  {
    for (Iterator iterator = delegateProperties.iterator() ; iterator.hasNext() ;)
    {
      Property property = (Property)iterator.next();
      if (dataObject.isSet(property))
        if (isSequenceProperty(property))
        {
          Sequence sequence = (Sequence)dataObject.get(property);
          int size = sequence.size();
          if (index < size)
            return sequence.getProperty(index);
          index -= size;
        }
        else if (property.isMany())
        {
          int size = dataObject.getList(property).size();
          if (index < size)
            return property;
          index -= size;
        }
        else if (index == 0)
          return property;
        else
          --index;
    }
    throw new IndexOutOfBoundsException();
  }

  public Object getValue(int index)
  {
    for (Iterator iterator = delegateProperties.iterator() ; iterator.hasNext() ;)
    {
      Property property = (Property)iterator.next();
      if (dataObject.isSet(property))
        if (isSequenceProperty(property))
        {
          Sequence sequence = (Sequence)dataObject.get(property);
          int size = sequence.size();
          if (index < size)
            return sequence.getValue(index);
          index -= size;
        }
        else if (property.isMany())
        {
          List values = dataObject.getList(property);
          int size = values.size();
          if (index < size)
            return values.get(index);
          index -= size;
        }
        else if (index == 0)
          return dataObject.get(property);
        else
          --index;
    }
    throw new IndexOutOfBoundsException();
  }

  public Object setValue(int index, Object value)
  {
    for (Iterator iterator = delegateProperties.iterator() ; iterator.hasNext() ;)
    {
      Property property = (Property)iterator.next();
      if (dataObject.isSet(property))
        if (isSequenceProperty(property))
        {
          Sequence sequence = (Sequence)dataObject.get(property);
          int size = sequence.size();
          if (index < size)
            return sequence.setValue(index, value);
          index -= size;
        }
        else if (property.isMany())
        {
          List values = dataObject.getList(property);
          int size = values.size();
          if (index < size)
            return values.set(index, value);
          index -= size;
        }
        else if (index == 0)
        {
          Object old = dataObject.get(property);  
          dataObject.set(property, value);
          return old;
        }
        else
          --index;
    }
    throw new IndexOutOfBoundsException();
  }
  
  boolean validate(EStructuralFeature feature, Object property)
  {
    return FeatureMapUtil.getValidator((EClass)dataObject.getType(), feature).isValid((EStructuralFeature)property);
  }

  boolean append(Property delegateProperty, Property property, Object value)
  {
    return ((Sequence)dataObject.get(delegateProperty)).add(property, value);
  }
  
  boolean append(Property property, Object value)
  {
    return dataObject.getList(property).add(value);
  }
  
  boolean set(Property property, Object value)
  {
    if (property.isMany())
      return append(property, value);
    dataObject.set(property, value);
    return true;
  }
  
  public final boolean add(Property p, Object value)
  {
    Property property;
    int size = delegateProperties.size(), index = size;
    do
    {
      property = (Property)delegateProperties.get(--index);
      if (!dataObject.isSet(property))
        continue;
      EStructuralFeature feature = (EStructuralFeature)property;
      if (FeatureMapUtil.isFeatureMap(feature))
      {
        if (validate(feature, p))
          return append(property, p, value);
      }
      else if (property == p && property.isMany())
        return append(property, value);
      if (size == ++index)
        return false;
      property = (Property)delegateProperties.get(index);
      break;
    }
    while (index != 0);
    for (;;)
    {
      EStructuralFeature feature = (EStructuralFeature)property;
      if (FeatureMapUtil.isFeatureMap(feature))
      {
        if (validate(feature, p))
          return append(property, p, value);
      }
      else if (property == p)
        return set(p, value);
      if (size == ++index)
        return false;
      property = (Property)delegateProperties.get(index);
    }
  }

  protected final Property property(String name)
  {
    return dataObject.getType().getProperty(name);
  }

  public boolean add(String propertyName, Object value)
  {
    return add(property(propertyName), value);
  }

  protected final Property property(int index)
  {
    return (Property)dataObject.getType().getProperties().get(index);
  }

  public boolean add(int propertyIndex, Object value)
  {
    return add(property(propertyIndex), value);
  }
  
  void insert(Property property, Property p, Object value)
  {
    ((Sequence)dataObject.get(property)).add(0, p, value);
  }
  
  void insert(Property property, Object value)
  {
    dataObject.getList(property).add(0, value);
  }
  
  protected final int insert(Iterator iterator, Property p, Object value)
  {
    while (iterator.hasNext())
    {
      Property property = (Property)iterator.next();
      EStructuralFeature feature = (EStructuralFeature)property;
      if (dataObject.isSet(property))
      {
        if (FeatureMapUtil.isFeatureMap(feature))
        {
          if (!validate(feature, p))
            return 2;
          insert(property, p, value);
          return 0;
        }
        if (property != p || !property.isMany())
          return 2;
        insert(property, value);
        return 0;
      }
      if (FeatureMapUtil.isFeatureMap(feature))
      {
        if (validate(feature, p))
        {
          append(property, p, value);
          return 0;
        }
      }
      else if (property == p)
      {
        set(property, value);
        return 0;
      }
    } // iterator.hasNext()
    return 1;
  }
  
  public final void add(int index, Property p, Object value)
  {
    Iterator iterator = delegateProperties.iterator();
    if (index == 0)
      switch (insert(iterator, p, value))
      {
        case 0:
          return;
        case 1:  
          throw new IndexOutOfBoundsException();
        default: // 2  
          throw new IllegalArgumentException();
      }
    while (iterator.hasNext())
    {
      Property property = (Property)iterator.next();
      if (dataObject.isSet(property))
        if (isSequenceProperty(property))
        {
          Sequence sequence = (Sequence)dataObject.get(property);
          int size = sequence.size();
          if (index < size)
          {
            sequence.add(index, p, value);
            return;
          }
          index -= size;
          if (index != 0)
            continue;
          if (insert(iterator, p, value) != 0)
            /*assert */sequence.add(p, value);
          return;
        } // sequence(property)
        else if (property.isMany())
        {
          List values = dataObject.getList(property);
          int size = values.size();
          if (index < size)
          {
            values.add(index, value);
            return;
          }
          index -= size;
          if (index == 0 && property == p)
          {
            values.add(value);
            return;
          }
        }
        else if (index == 0)
          throw new IllegalArgumentException();
        else
          --index;
    }
    throw new IndexOutOfBoundsException();
  }

  public void add(int index, String propertyName, Object value)
  {
    add(index, property(propertyName), value);
  }

  public void add(int index, int propertyIndex, Object value)
  {
    add(index, property(propertyIndex), value);
  }

  public final void addText(String text)
  {
    throw new IllegalArgumentException();
  }

  /**
   * @deprecated
   */
  public void add(String text)
  {
    addText(text);
  }

  public final void addText(int index, String text)
  {
    throw new IllegalArgumentException();
  }

  /**
   * @deprecated
   */
  public void add(int index, String text)
  {
    addText(index, text);
  }

  public void remove(int index)
  {
    for (Iterator iterator = delegateProperties.iterator() ; iterator.hasNext() ;)
    {
      Property property = (Property)iterator.next();
      if (dataObject.isSet(property))
        if (isSequenceProperty(property))
        {
          Sequence sequence = (Sequence)dataObject.get(property);
          int size = sequence.size();
          if (index < size)
          {
            sequence.remove(index);
            return;
          }
          index -= size;
        }
        else if (property.isMany())
        {
          List values = dataObject.getList(property);
          int size = values.size();
          if (index < size)
          {
            values.remove(index);
            return;
          }
          index -= size;
        }
        else if (index == 0)
        {
          dataObject.unset(property);
          return;
        }
        else
          --index;
    }
    throw new IndexOutOfBoundsException();
  }

  static private Object remove(Sequence sequence, int index)
  {
    Object value = sequence.getValue(index);
    sequence.remove(index);
    return value;
  }
  
  static void move(Sequence fromSequence, int fromIndex, Sequence toSequence, int toIndex, Property property)
  {
    toSequence.add(toIndex, property, remove(fromSequence, fromIndex)); // removes containment
  }
  
  static protected void move(Sequence fromSequence, int fromIndex, Sequence toSequence, int toIndex)
  {
    move(fromSequence, fromIndex, toSequence, toIndex, fromSequence.getProperty(fromIndex));
  }
  
  static protected void add(Object value,List list,int index, int size)
  {
    if (++index == size)
      /*assert */list.add(value);
    else
      list.add(index, value); // removes containment
  }
  
  static protected void add(Property property, Object value, Sequence toSequence, int toIndex, int size)
  {
    if (++toIndex == size)
      toSequence.add(property, value); // removes containment
    else
      toSequence.add(toIndex, property, value); // removes containment
  }
  
  public void move(int toIndex, int fromIndex)
  {
    for (Iterator iterator = delegateProperties.iterator(); iterator.hasNext() ;)
    {
      Property property = (Property)iterator.next();
      if (dataObject.isSet(property))
        if (isSequenceProperty(property))
        {
          Sequence sequence = (Sequence)dataObject.get(property);
          int size = sequence.size();
          if (toIndex < size)
          {
            if (fromIndex < size)
            {
              sequence.move(toIndex, fromIndex);
              return;
            }
            while (iterator.hasNext())
            {
              property = (Property)iterator.next();
              if (!dataObject.isSet(property))
                continue;
              fromIndex -= size;
              if (isSequenceProperty(property))
              {
                Sequence fromSequence = (Sequence)dataObject.get(property);
                size = fromSequence.size();
                if (fromIndex >= size)
                  continue;
                move(fromSequence, fromIndex, sequence, toIndex);
                return;
              }
              if (property.isMany())
              {
                List list = dataObject.getList(property);
                size = list.size();
                if (fromIndex >= size)
                  continue;
                sequence.add(toIndex, property, list.remove(fromIndex)); // removes containment
                return;
              }
              if (fromIndex == 0)
              {
                sequence.add(toIndex, property, dataObject.get(property)); // removes containment
                dataObject.unset(property);
                return;
              }
              size = 1;
            } // iterator.hasNext()
            break;
          } // toIndex < size
          if (fromIndex < size)
          {
            while (iterator.hasNext())
            {
              property = (Property)iterator.next();
              if (!dataObject.isSet(property))
                continue;
              toIndex -= size;
              if (isSequenceProperty(property))
              {
                Sequence toSequence = (Sequence)dataObject.get(property);
                size = toSequence.size();
                if (toIndex >= size)
                  continue;
                if (++toIndex == size)
                  toSequence.add(sequence.getProperty(fromIndex), remove(sequence, fromIndex)); // Java pushes stack from left to right
                    // removes containment
                else
                  move(sequence, fromIndex, toSequence, toIndex);
                return;
              }
              if (property.isMany())
              {
                List list = dataObject.getList(property);
                size = list.size();
                if (toIndex >= size)
                  continue;
                if (sequence.getProperty(fromIndex) != property)
                  throw new IllegalArgumentException();
                add(remove(sequence, fromIndex), list, toIndex, size);
                return;
              }
              if (toIndex == 0)
              {
                while (iterator.hasNext())
                {
                  Property p = sequence.getProperty(fromIndex);
                  property = (Property)iterator.next();
                  EStructuralFeature feature = (EStructuralFeature)property;
                  if (dataObject.isSet(property))
                  {
                    if (FeatureMapUtil.isFeatureMap(feature))
                    {
                      /*if (!validate(feature, p))
                        throw new IllegalArgumentException(); */
                      move(sequence, fromIndex, (Sequence)dataObject.get(property), 0, p);
                      return;
                    }
                    if (property != p || !property.isMany())
                      throw new IllegalArgumentException();
                    insert(property, remove(sequence, fromIndex)); // removes containment
                    return;
                  }
                  if (FeatureMapUtil.isFeatureMap(feature))
                  {
                    if (validate(feature, p))
                    {
                      append(property, p, remove(sequence, fromIndex)); // removes containment
                      return;
                    }
                  }
                  else if (property == p)
                  {
                    set(property, remove(sequence, fromIndex));
                    return;
                  }
                } // iterator.hasNext()
                break;
              }
              size = 1;
            } // iterator.hasNext()
            break;
          } // fromIndex < size
          toIndex -= size;
          fromIndex -= size;
        } // sequence(property)
        else if (property.isMany())
        {
          List list = dataObject.getList(property);
          int size = list.size();
          if (toIndex < size)
          {
            if (fromIndex < size)
            {
              ((EList)list).move(toIndex, fromIndex);
              return;
            }
            while (iterator.hasNext())
            {
              Property p = (Property)iterator.next();
              if (!dataObject.isSet(p))
                continue;
              fromIndex -= size;
              if (isSequenceProperty(p))
              {
                Sequence fromSequence = (Sequence)dataObject.get(p);
                size = fromSequence.size();
                if (fromIndex >= size)
                  continue;
                if (fromSequence.getProperty(fromIndex) != property)
                  throw new IllegalArgumentException();
                list.add(toIndex, remove(fromSequence, fromIndex)); // removes containment
                return;
              }
              if (p.isMany())
              {
                List l = dataObject.getList(p);
                size = l.size();
                if (fromIndex >= size)
                  continue;
                /*if (p != property)
                  throw new IllegalArgumentException(); */
                list.add(toIndex, l.remove(fromIndex)); // removes containment
                return;
              }
              if (fromIndex == 0)
              {
                /*if (p != property)
                throw new IllegalArgumentException(); */
                list.add(toIndex, dataObject.get(p)); // removes containment
                dataObject.unset(p);
                return;
              }
              size = 1;
            } // iterator.hasNext()
            break;
          } // toIndex < size
          if (fromIndex < size)
          {
            while (iterator.hasNext())
            {
              Property p = (Property)iterator.next();
              if (!dataObject.isSet(p))
                continue;
              toIndex -= size;
              if (isSequenceProperty(p))
              {
                Sequence toSequence = (Sequence)dataObject.get(p);
                size = toSequence.size();
                if (toIndex >= size)
                  continue;
                add(property, list.remove(fromIndex), toSequence, toIndex, size);
                return;
              }
              if (p.isMany())
              {
                List l = dataObject.getList(p);
                size = l.size();
                if (toIndex >= size)
                  continue;
                /*if (property != p)
                  throw new IllegalArgumentException(); */
                add(list.remove(fromIndex), l, toIndex, size);
                return;
              }
              if (toIndex == 0)
              {
                while (iterator.hasNext())
                {
                  p = (Property)iterator.next();
                  EStructuralFeature feature = (EStructuralFeature)p;
                  if (dataObject.isSet(p))
                  {
                    if (FeatureMapUtil.isFeatureMap(feature))
                    {
                      /*if (!validate(feature, property))
                        throw new IllegalArgumentException(); */
                      insert(p, property, list.remove(fromIndex));
                      return;
                    }
                    if (/*p != property || */!p.isMany())
                      throw new IllegalArgumentException();
                    insert(p, list.remove(fromIndex)); // removes containment
                    return;
                  }
                  if (FeatureMapUtil.isFeatureMap(feature))
                  {
                    if (!validate(feature, property))
                      continue;
                    append(p, property, list.remove(fromIndex)); // removes containment
                    return;
                  }
                  else if (p == property)
                  {
                    set(p, list.remove(fromIndex));
                    return;
                  }
                } // iterator.hasNext()
                break;
              } // toIndex == 0
              size = 1;
            } // iterator.hasNext()
            break;
          } // fromIndex < size
          toIndex -= size;
          fromIndex -= size;
        } // property.isMany()
        else if (toIndex == 0)
          throw new IllegalArgumentException();
        else if (fromIndex == 0)
        {
          for (int size = 1; iterator.hasNext() ;)
          {
            Property p = (Property)iterator.next();
            if (!dataObject.isSet(p))
              continue;
            toIndex -= size;
            if (isSequenceProperty(p))
            {
              Sequence toSequence = (Sequence)dataObject.get(p);
              size = toSequence.size();
              if (toIndex >= size)
                continue;
              add(property, dataObject.get(property), toSequence, toIndex, size);
              dataObject.unset(property);
              return;
            }
            if (p.isMany())
            {
              List l = dataObject.getList(p);
              size = l.size();
              if (toIndex >= size)
                continue;
              /*if (property != p)
                throw new IllegalArgumentException(); */
              add(dataObject.get(property), l, toIndex, size);
              dataObject.unset(property);
              return;
            }
            if (toIndex == 0)
            {
              while (iterator.hasNext())
              {
                p = (Property)iterator.next();
                EStructuralFeature feature = (EStructuralFeature)p;
                if (dataObject.isSet(p))
                  if (FeatureMapUtil.isFeatureMap(feature))
                    /*if (!validate(feature, property))
                      throw new IllegalArgumentException(); */
                    insert(p, property, dataObject.get(property));
                  else if (/*p == property && */p.isMany())
                    insert(p, dataObject.get(property)); // removes containment
                  else
                    throw new IllegalArgumentException();
                else if (FeatureMapUtil.isFeatureMap(feature))
                {
                  if (!validate(feature, property))
                    continue;
                  append(p, property, dataObject.get(property)); // removes containment
                }
                else if (p == property)
                  set(p, dataObject.get(property));
                else
                  continue;
                dataObject.unset(property);
                return;
              } // iterator.hasNext()
              break;
            } // toIndex == 0
            size = 1;
          } // iterator.hasNext()
          break;
        } // fromIndex == 0
        else
        {
          --toIndex;
          --fromIndex;
        }
    }
    throw new IndexOutOfBoundsException();
  }
}