001    package com.hammurapi.review.util;
002    
003    import java.util.Iterator;
004    
005    import org.eclipse.emf.common.util.TreeIterator;
006    import org.eclipse.emf.ecore.EObject;
007    
008    import com.hammurapi.convert.AtomicConverterBase;
009    import com.hammurapi.convert.Converter;
010    import com.hammurapi.util.Context;
011    
012    /**
013     * Converts EObject to iterable which includes the object itself into iteration.
014     * @author Pavel Vlasov
015     *
016     */
017    public class EObjectToIterableConverter extends AtomicConverterBase<EObject, Iterable<EObject>> {
018            
019            @SuppressWarnings("unchecked")
020            public EObjectToIterableConverter() {
021                    super(EObject.class, (Class<Iterable<EObject>>) (Class) Iterable.class);
022            }       
023    
024            @Override
025            public Iterable<EObject> convert(final EObject source, Converter master, Context context, ClassLoader classLoader) {
026                    return new Iterable<EObject>() {
027                            @Override
028                            public Iterator<EObject> iterator() {
029                                    return new TreeIterator<EObject>() {
030                                            private TreeIterator<EObject> master;
031                                            private boolean pruned;
032    
033                                            @Override
034                                            public void prune() {
035                                                    if (master==null) {
036                                                            pruned=true;
037                                                    } else {
038                                                            master.prune();
039                                                    }
040                                            }
041    
042                                            @Override
043                                            public boolean hasNext() {
044                                                    return master==null ? !pruned : master.hasNext();
045                                            }
046    
047                                            @Override
048                                            public EObject next() {
049                                                    if (master==null) {
050                                                            if (pruned) {
051                                                                    throw new IllegalStateException();
052                                                            } else {
053                                                                    master = source.eAllContents();
054                                                                    return source;
055                                                            }
056                                                    }
057                                                    
058                                                    return master.next();
059                                            }
060    
061                                            @Override
062                                            public void remove() {
063                                                    if (master==null) {
064                                                            throw new IllegalStateException();
065                                                    } else {
066                                                            master.remove();
067                                                    }
068                                            }
069                                            
070                                    };
071                            }
072                            
073                    };
074            }
075    
076    }