在scala中实现有序特征

时间:2016-09-08 06:08:30

标签: scala

我已经实现了一个带有有序特征的scala类..

page.render(renderContext).then(function(){

    // code for converting data url

});


    <!DOCTYPE HTML>
    <html>
    <head>
    <script type='text/javascript' src='//code.jquery.com/jquery-2.2.3.js'></script>
    <script type="text/javascript" src="https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>
    </head>
    <body>
    <div id='anteprima' ></div>
    <div id='anteprima_img' ></div>

    <script type="text/javascript">
    var file = 'http://www.businessmodelgeneration.com/downloads/businessmodelgeneration_preview.pdf';
    pdftoimg(file, 1, 0.5, 'anteprima');

    function pdftoimg(file, num, scale2, idd) {
        PDFJS.disableWorker = true;
        PDFJS.getDocument(file).then(function(pdf) {
            pdf.getPage(num).then(function(page) {
                var canvas = document.createElement('canvas');
                canvas.id = 'pag' + num;
                canvas.className = 'grande';
                canvasContainer = document.getElementById(idd);
                var context = canvas.getContext('2d');
                var viewport = page.getViewport(scale2);
                canvas.height = viewport.height;
                canvas.width = viewport.width;
                var renderContext = {
                    canvasContext: context,
                    viewport: viewport
                };
                page.render(renderContext).then(function(){
                    canvasContainer.appendChild(canvas);
                    var dataUrl = canvas.toDataURL();
                    image = document.createElement('img');
                    image.src = dataUrl;
                    $('#anteprima_img').html(image);
                });
            });
        });
    };

    </script>
    </body>
    </html>

在第二节课我得到参数START_TIME需要覆盖修饰符..

1 个答案:

答案 0 :(得分:2)

好吧,如果CDR_NOR是抽象的,你可以这样定义:

abstract class CDR_NOR extends Ordered [CDR_NOR] {
  def SUBSCRIBER_ID:String
  def CHARGING_ID:String
  def NODE_ID:String
  def START_TIME:String

  def compare(that: CDR_NOR) = {
   if (this.START_TIME.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+") && 
       that.START_TIME.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+")) {

      val formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss:SSS")
      val d1 = formatter.parseDateTime(this.START_TIME)
      val d2 = formatter.parseDateTime(that.START_TIME)

      d1.compareTo(d2)
    } else {
      0
    }
  }
}

case class DXE_NOR(SUBSCRIBER_ID:String, CHARGING_ID:String, NODE_ID:String,
               START_TIME:String, DXE_PROTOCOL_ID:String, DXE_SUB_PROTOCOL_ID:String, DXE_APPLICATION_ID:String, DXE_INTERNET_SERVER_IP:String,
               DXE_SENDER_ID: String, DXE_USAGE_TYPE: String, DXE_FILE_Population_TIME: String, DXE_IMSI: String, DXE_MSISDN: String, DXE_IMEI: String, DXE_CHARGING1_ID: String, DXE_UE_IP_ADDRESS: String,
               DXE_SESSION_ID: String, DXE_BEARER_CREATION_TIME: String, DXE_EVENT_START_TIME: String, DXE_EVENT_END_TIME: String, DXE_DURATION: String, DXE_UPLINK_VOL: String, DXE_DOWNLINK_VOL: String, DXE_TOTAL_VOL: String, DXE_RAT_TYPE: String,
               DXE_CELL_ID: String, DXE_ECELL_ID: String, DXE_HPMN: String, DXE_VPMN: String, DXE_SUBSCRIBER_TYPE: String, DXE_ROAM_TYPE: String, DXE_HANDSET_CATEGORY: String, DXE_GATEWAY_IP_ADDRESS: String,
               DXE_SERVING_IP_ADDRESS: String, DXE_APN: String, DXE_APN_TYPE: String, DXE_APP_NAME: String, DXE_DOMAIN_NAME: String,
               DXE_HTTP_PROXY_SERVER_IP_ADDRESS: String, DXE_PROXY_DOMAIN_NAME: String, DXE_CHARGING_CHARACTERISTICS: String, DXE_APN_RA_FLAG: String, DXE_APP_RA_FLAG: String, DXE_URL_RA_FLAG: String, DXE_Original_FILE_NAME: String
              ) extends CDR_NOR

<强>更新 但是,afaik通常建议您更喜欢Ordering以上的Ordered

abstract class CDR_NOR {
  def SUBSCRIBER_ID: String

  def CHARGING_ID: String

  def NODE_ID: String

  def START_TIME: String
}

object CDR_NOR {
  implicit object CdrOrdering extends Ordering[CDR_NOR] {
    def compare(x: CDR_NOR, y: CDR_NOR): Int = {
      if (x.START_TIME.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+") &&
        y.START_TIME.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+")) {

        val formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss:SSS")
        val d1 = formatter.parseDateTime(x.START_TIME)
        val d2 = formatter.parseDateTime(y.START_TIME)

        d1.compareTo(d2)
      } else {
        0
      }
    }
  }
}

对于sorted,您必须确保implicit Ordering在范围内。要使用运算符,您必须导入Ordering.Implicits._