github.com/dubbogo/gost@v1.14.0/gof/observer/event.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package observer
    19  
    20  import (
    21  	"fmt"
    22  	"math/rand"
    23  	"time"
    24  )
    25  
    26  func init() {
    27  	rand.Seed(time.Now().UnixNano())
    28  }
    29  
    30  // Event is align with Event interface in Java.
    31  // it's the top abstraction
    32  // Align with 2.7.5
    33  type Event interface {
    34  	fmt.Stringer
    35  	GetSource() interface{}
    36  	GetTimestamp() time.Time
    37  }
    38  
    39  // BaseEvent is the base implementation of Event
    40  // You should never use it directly
    41  type BaseEvent struct {
    42  	Source    interface{}
    43  	Timestamp time.Time
    44  }
    45  
    46  // GetSource return the source
    47  func (b *BaseEvent) GetSource() interface{} {
    48  	return b.Source
    49  }
    50  
    51  // GetTimestamp return the Timestamp when the event is created
    52  func (b *BaseEvent) GetTimestamp() time.Time {
    53  	return b.Timestamp
    54  }
    55  
    56  // String return a human readable string representing this event
    57  func (b *BaseEvent) String() string {
    58  	return fmt.Sprintf("BaseEvent[source = %#v]", b.Source)
    59  }
    60  
    61  // NewBaseEvent create an BaseEvent instance
    62  // and the Timestamp will be current timestamp
    63  func NewBaseEvent(source interface{}) *BaseEvent {
    64  	return &BaseEvent{
    65  		Source:    source,
    66  		Timestamp: time.Now(),
    67  	}
    68  }