Skip to content

Commit

Permalink
update Identifiable
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed May 7, 2015
1 parent 14502d5 commit eaeed35
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
26 changes: 21 additions & 5 deletions mllib/src/main/scala/org/apache/spark/ml/util/Identifiable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,31 @@ package org.apache.spark.ml.util

import java.util.UUID


/**
* Object with a unique id.
* Object with a unique ID that identifies itself and its derivatives.
*/
private[ml] trait Identifiable extends Serializable {

/**
* A unique id for the object. The default implementation concatenates the class name, "_", and 8
* random hex chars.
* A unique ID for the object and its derivatives. The default implementation concatenates
* [[simpleClassName]], "_", and 8 random hex chars.
*/
final def uid: String = _uid

/**
* A simple name of the class, which is used as the first part of the generated UID. The default
* implementation uses [[java.lang.Class#getSimpleName()]].
*/
private[ml] val uid: String =
this.getClass.getSimpleName + "_" + UUID.randomUUID().toString.take(8)
protected def simpleClassName: String = this.getClass.getSimpleName

/**
* Sets the UID of the instance.
*/
protected final def setUID(uid: String): this.type = {
this._uid = uid
this
}

private var _uid = simpleClassName + "_" + UUID.randomUUID().toString.take(8)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.spark.ml.util

import org.scalatest.FunSuite

class IdentifiableSuite extends FunSuite {

import IdentifiableSuite._

test("Identifiable") {
val test0 = new Test0
assert(test0.uid.startsWith(classOf[Test0].getSimpleName + "_"))

val test1 = new Test1
assert(test1.uid.startsWith("test_"),
"simpleClassName should be the first part of the generated UID.")
val copied = test1.copy
assert(copied.uid === test1.uid, "Copied objects should be able to use the same UID.")
}
}

object IdentifiableSuite {

class Test0 extends Identifiable

class Test1 extends Identifiable {

override def simpleClassName: String = "test"

def copy: Test1 = {
new Test1().setUID(uid)
}
}
}

0 comments on commit eaeed35

Please sign in to comment.