001    /*
002     * Copyright (c) 2007-2015 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.pipe;
022    
023    import cascading.CascadingException;
024    import cascading.tuple.Fields;
025    import cascading.util.TraceUtil;
026    
027    /** Class OperatorException is thrown during field name resolution during planning */
028    public class OperatorException extends CascadingException
029      {
030      enum Kind
031        {
032          argument, grouping, sorting, joining
033        }
034    
035      private Fields incomingFields;
036      private Fields argumentSelector;
037      private Fields groupingSelector;
038      private Fields sortingSelector;
039      private Fields declaredFields;
040      private Fields outputSelector;
041    
042      /** @see cascading.CascadingException#CascadingException() */
043      public OperatorException()
044        {
045        }
046    
047      /**
048       * Constructor OperatorException creates a new OperatorException instance.
049       *
050       * @param pipe   of type Pipe
051       * @param string of type String
052       */
053      public OperatorException( Pipe pipe, String string )
054        {
055        super( TraceUtil.formatTrace( pipe, string ) );
056        }
057    
058      /**
059       * Constructor OperatorException creates a new OperatorException instance.
060       *
061       * @param pipe      of type Pipe
062       * @param string    of type String
063       * @param throwable of type Throwable
064       */
065      public OperatorException( Pipe pipe, String string, Throwable throwable )
066        {
067        super( TraceUtil.formatTrace( pipe, string ), throwable );
068        }
069    
070      /** @see cascading.CascadingException#CascadingException(String) */
071      protected OperatorException( String string )
072        {
073        super( string );
074        }
075    
076      /** @see cascading.CascadingException#CascadingException(String, Throwable) */
077      protected OperatorException( String string, Throwable throwable )
078        {
079        super( string, throwable );
080        }
081    
082      /** @see cascading.CascadingException#CascadingException(Throwable) */
083      protected OperatorException( Throwable throwable )
084        {
085        super( throwable );
086        }
087    
088      /**
089       * Constructor OperatorException creates a new OperatorException instance.
090       *
091       * @param pipe           of type Pipe
092       * @param incomingFields of type Fields
093       * @param declaredFields of type Fields
094       * @param outputSelector of type Fields
095       * @param throwable      of type Throwable
096       */
097      public OperatorException( Pipe pipe, Fields incomingFields, Fields declaredFields, Fields outputSelector, Throwable throwable )
098        {
099        super( createMessage( pipe, incomingFields, declaredFields, outputSelector ), throwable );
100    
101        this.incomingFields = incomingFields;
102        this.declaredFields = declaredFields;
103        this.outputSelector = outputSelector;
104        }
105    
106      /**
107       * Constructor OperatorException creates a new OperatorException instance.
108       *
109       * @param pipe           of type Pipe
110       * @param kind           of type Kind
111       * @param incomingFields of type Fields
112       * @param selectorFields of type Fields
113       * @param throwable      of type Throwable
114       */
115      public OperatorException( Pipe pipe, Kind kind, Fields incomingFields, Fields selectorFields, Throwable throwable )
116        {
117        super( createMessage( pipe, kind, incomingFields, selectorFields ), throwable );
118    
119        this.incomingFields = incomingFields;
120    
121        if( kind == Kind.argument )
122          this.argumentSelector = selectorFields;
123        else if( kind == Kind.grouping )
124          this.groupingSelector = selectorFields;
125        else
126          this.sortingSelector = selectorFields;
127        }
128    
129      /**
130       * Method getIncomingFields returns the incomingFields of this OperatorException object.
131       *
132       * @return the incomingFields (type Fields) of this OperatorException object.
133       */
134      public Fields getIncomingFields()
135        {
136        return incomingFields;
137        }
138    
139      /**
140       * Method getArgumentSelector returns the argumentSelector of this OperatorException object.
141       *
142       * @return the argumentSelector (type Fields) of this OperatorException object.
143       */
144      public Fields getArgumentSelector()
145        {
146        return argumentSelector;
147        }
148    
149      /**
150       * Method getGroupingSelector returns the groupingSelector of this OperatorException object.
151       *
152       * @return the groupingSelector (type Fields) of this OperatorException object.
153       */
154      public Fields getGroupingSelector()
155        {
156        return groupingSelector;
157        }
158    
159      /**
160       * Method getSortingSelector returns the sortingSelector of this OperatorException object.
161       *
162       * @return the sortingSelector (type Fields) of this OperatorException object.
163       */
164      public Fields getSortingSelector()
165        {
166        return sortingSelector;
167        }
168    
169      /**
170       * Method getDeclaredFields returns the declaredFields of this OperatorException object.
171       *
172       * @return the declaredFields (type Fields) of this OperatorException object.
173       */
174      public Fields getDeclaredFields()
175        {
176        return declaredFields;
177        }
178    
179      /**
180       * Method getOutputSelector returns the outputSelector of this OperatorException object.
181       *
182       * @return the outputSelector (type Fields) of this OperatorException object.
183       */
184      public Fields getOutputSelector()
185        {
186        return outputSelector;
187        }
188    
189      private static String createMessage( Pipe pipe, Fields incomingFields, Fields declaredFields, Fields outputSelector )
190        {
191        String message = "unable to resolve output selector: " + outputSelector.printVerbose() +
192          ", with incoming: " + incomingFields.printVerbose() + " and declared: " + declaredFields.printVerbose();
193    
194        return TraceUtil.formatTrace( pipe, message );
195        }
196    
197      private static String createMessage( Pipe pipe, Kind kind, Fields incomingFields, Fields argumentSelector )
198        {
199        String message = "unable to resolve " + kind + " selector: " + argumentSelector.printVerbose() +
200          ", with incoming: " + incomingFields.printVerbose();
201    
202        return TraceUtil.formatTrace( pipe, message );
203        }
204    
205      }