Coverage Report - org.apache.giraph.jython.wrappers.JythonWritableWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
JythonWritableWrapper
0%
0/38
0%
0/12
3
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.giraph.jython.wrappers;
 19  
 
 20  
 import org.apache.hadoop.io.WritableComparable;
 21  
 import org.apache.hadoop.io.WritableUtils;
 22  
 import org.apache.log4j.Logger;
 23  
 import org.python.core.PyException;
 24  
 import org.python.core.PyObject;
 25  
 import org.python.core.PyString;
 26  
 import org.python.modules.cPickle;
 27  
 
 28  
 import com.google.common.base.Preconditions;
 29  
 
 30  
 import java.io.DataInput;
 31  
 import java.io.DataOutput;
 32  
 import java.io.IOException;
 33  
 
 34  
 /**
 35  
  * Wraps a Jython object, adding {@link WritableComparable} interface.
 36  
  * Used for graph types (IVEMM) that are in pure Jython.
 37  
  */
 38  
 public class JythonWritableWrapper extends JythonWrapperBase
 39  
     implements WritableComparable {
 40  
   /** Logger */
 41  0
   private static final Logger LOG =
 42  0
       Logger.getLogger(JythonWritableWrapper.class);
 43  
 
 44  
   /**
 45  
    * Constructor
 46  
    *
 47  
    * @param pyObject jython object to wrap
 48  
    */
 49  
   public JythonWritableWrapper(PyObject pyObject) {
 50  0
     super(pyObject);
 51  0
   }
 52  
 
 53  
   @Override
 54  
   public void readFields(DataInput in) throws IOException {
 55  0
     String str = WritableUtils.readString(in);
 56  0
     PyString pyString = new PyString(str);
 57  
     Object object;
 58  
     try {
 59  0
       object = cPickle.loads(pyString);
 60  0
     } catch (PyException e) {
 61  0
       LOG.fatal("Could not deserialize Jython value from string " + str);
 62  0
       throw e;
 63  0
     }
 64  0
     Preconditions.checkArgument(object instanceof PyObject);
 65  0
     setPyObject((PyObject) object);
 66  0
   }
 67  
 
 68  
   @Override
 69  
   public void write(DataOutput out) throws IOException {
 70  
     PyString pyString;
 71  
     try {
 72  0
       pyString = cPickle.dumps(getPyObject());
 73  0
     } catch (PyException e) {
 74  0
       LOG.fatal("Could not serialize wrapped Jython value: " +
 75  0
           getPyObject().__str__());
 76  0
       throw e;
 77  0
     }
 78  0
     WritableUtils.writeString(out, pyString.getString());
 79  0
   }
 80  
 
 81  
   @Override
 82  
   public int compareTo(Object other) {
 83  0
     int result = -1;
 84  0
     if (other instanceof PyObject) {
 85  0
       PyObject pyOther = (PyObject) other;
 86  0
       result = getPyObject().__cmp__(pyOther);
 87  0
     } else if (JythonWritableWrapper.class.equals(other.getClass())) {
 88  0
       JythonWritableWrapper wrapperOther = (JythonWritableWrapper) other;
 89  0
       result = getPyObject().__cmp__(wrapperOther.getPyObject());
 90  
     }
 91  0
     return result;
 92  
   }
 93  
 
 94  
   @Override
 95  
   public boolean equals(Object obj) {
 96  0
     if (obj == null) {
 97  0
       return false;
 98  
     }
 99  0
     if (this == obj) {
 100  0
       return true;
 101  
     }
 102  0
     if (obj instanceof JythonWritableWrapper) {
 103  0
       return compareTo(obj) == 0;
 104  
     }
 105  0
     return false;
 106  
   }
 107  
 
 108  
   @Override
 109  
   public int hashCode() {
 110  0
     return getPyObject().__hash__().asInt();
 111  
   }
 112  
 }