Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.mahout.common;
 
 import java.util.List;
 
 import  org.apache.hadoop.hbase.util.Bytes;
 import  org.apache.hadoop.io.WritableComparable;

An Ordered List of Strings which can be used in a Hadoop Map/Reduce Job
 
 public final class StringTuple implements WritableComparable<StringTuple> {
 
   private List<Stringtuple = new ArrayList<String>();
 
   public StringTuple() {
   }
 
   public StringTuple(String firstEntry) {
     add(firstEntry);
   }
   
   public StringTuple(Iterable<Stringentries) {
     for (String entry : entries) {
       add(entry);
     }
   }
 
   public StringTuple(String[] entries) {
     for (String entry : entries) {
       add(entry);
     }
   }

  
add an entry to the end of the list

Parameters:
entry
Returns:
true if the items get added
 
   public boolean add(String entry) {
     return .add(entry);
   }

  
Fetches the string at the given location

Parameters:
index
Returns:
String value at the given location in the tuple list
 
   public String stringAt(int index) {
     return .get(index);
   }

  
Replaces the string at the given index with the given newString

Parameters:
index
newString
Returns:
The previous value at that location
 
   public String replaceAt(int indexString newString) {
     return .set(indexnewString);
   }

  
Fetch the list of entries from the tuple

Returns:
a List containing the strings in the order of insertion
 
   public List<StringgetEntries() {
     return Collections.unmodifiableList(this.);
   }

  
Returns the length of the tuple

Returns:
length
  public int length() {
    return this..size();
  }
  public String toString() {
    return .toString();
  }
  public int hashCode() {
    return .hashCode();
  }
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    StringTuple other = (StringTupleobj;
    if ( == null) {
      if (other.tuple != null)
        return false;
    } else if (!.equals(other.tuple))
      return false;
    return true;
  }
  public void readFields(DataInput inthrows IOException {
    int len = in.readInt();
     = new ArrayList<String>(len);
    for (int i = 0; i < leni++) {
      int fieldLen = in.readInt();
      byte[] entry = new byte[fieldLen];
      in.readFully(entry);
      .add(Bytes.toString(entry));
    }
  }
  public void write(DataOutput outthrows IOException {
    out.writeInt(.size());
    for (String entry : ) {
      byte[] data = Bytes.toBytes(entry);
      out.writeInt(data.length);
      out.write(data);
    }
  }
  public int compareTo(StringTuple otherTuple) {
    int thisLength = length();
    int otherLength = otherTuple.length();
    int min = Math.min(thisLengthotherLength);
    for (int i = 0; i < mini++) {
      int ret = this..get(i).compareTo(otherTuple.stringAt(i));
      if (ret != 0) {
        return ret;
      }
    }
    if (thisLength < otherLength) {
      return -1;
    } else if (thisLength > otherLength) {
      return 1;
    } else {
      return 0;
    }
  }
New to GrepCode? Check out our FAQ X