• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 package com.example.executorchllamademo;
10 
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.Locale;
14 
15 public class Message {
16   private String text;
17   private final boolean isSent;
18   private float tokensPerSecond;
19   private long totalGenerationTime;
20   private final long timestamp;
21   private final MessageType messageType;
22   private String imagePath;
23   private final int promptID;
24 
25   private static final String TIMESTAMP_FORMAT = "hh:mm a"; // example: 2:23 PM
26 
Message(String text, boolean isSent, MessageType messageType, int promptID)27   public Message(String text, boolean isSent, MessageType messageType, int promptID) {
28     this.isSent = isSent;
29     this.messageType = messageType;
30     this.promptID = promptID;
31 
32     if (messageType == MessageType.IMAGE) {
33       this.imagePath = text;
34     } else {
35       this.text = text;
36     }
37 
38     if (messageType != MessageType.SYSTEM) {
39       this.timestamp = System.currentTimeMillis();
40     } else {
41       this.timestamp = (long) 0;
42     }
43   }
44 
getPromptID()45   public int getPromptID() {
46     return promptID;
47   }
48 
getMessageType()49   public MessageType getMessageType() {
50     return messageType;
51   }
52 
getImagePath()53   public String getImagePath() {
54     return imagePath;
55   }
56 
getText()57   public String getText() {
58     return text;
59   }
60 
appendText(String text)61   public void appendText(String text) {
62     this.text += text;
63   }
64 
getIsSent()65   public boolean getIsSent() {
66     return isSent;
67   }
68 
setTokensPerSecond(float tokensPerSecond)69   public void setTokensPerSecond(float tokensPerSecond) {
70     this.tokensPerSecond = tokensPerSecond;
71   }
72 
setTotalGenerationTime(long totalGenerationTime)73   public void setTotalGenerationTime(long totalGenerationTime) {
74     this.totalGenerationTime = totalGenerationTime;
75   }
76 
getTokensPerSecond()77   public float getTokensPerSecond() {
78     return tokensPerSecond;
79   }
80 
getTotalGenerationTime()81   public long getTotalGenerationTime() {
82     return totalGenerationTime;
83   }
84 
getTimestamp()85   public long getTimestamp() {
86     return timestamp;
87   }
88 
getFormattedTimestamp()89   public String getFormattedTimestamp() {
90     SimpleDateFormat formatter = new SimpleDateFormat(TIMESTAMP_FORMAT, Locale.getDefault());
91     Date date = new Date(timestamp);
92     return formatter.format(date);
93   }
94 }
95