<commit_before>// Copyright 2017 Google Inc.
//
// Licensed 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 codeu.chat.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashMap;
import java.util.ArrayList;

import codeu.chat.client.core.ConversationContext;
import codeu.chat.client.core.MessageContext;
import codeu.chat.util.Serializer;
import codeu.chat.util.Serializers;
import codeu.chat.util.Time;
import codeu.chat.util.Uuid;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

 

// ignore anything that may cause problems when converting to JSON
// prevents crashes for items that cant be serialized.
@JsonIgnoreProperties(ignoreUnknown=true)
public final class User {
  
  public HashMap<Uuid, Integer> conversationInterests = new HashMap<>();
  public ArrayList<Uuid> userInterests = new ArrayList<Uuid>();
	
  // no need to serialize the serializer when converting to JOSN
  @JsonIgnore
  public static final Serializer<User> SERIALIZER = new Serializer<User>() {

    @Override
    public void write(OutputStream out, User value) throws IOException {

      Uuid.SERIALIZER.write(out, value.id);
      Serializers.STRING.write(out, value.name);
      Time.SERIALIZER.write(out, value.creation);

    }

    @Override
    public User read(InputStream in) throws IOException {
      return new User(
          Uuid.SERIALIZER.read(in),
          Serializers.STRING.read(in),
          Time.SERIALIZER.read(in)
      );
    }
  };

  // ignore objects that aren't built-in types (string, int, etc)
  // Jackons will default to the getters since instance vars are ignored
  @JsonIgnore
  public final Uuid id;
  public final String name;
  @JsonIgnore
  public final Time creation;
  
  // time of user's last update recorded as a long
  // initially set to user's creation time 
  public long lastUpdateTime = Time.now().inMs(); 
  public static int CONVOS_CREATED_ARRAY = 0; 
  public static int CONVOS_CONTRIBUTED_TO_ARRAY = 1; 

  public User(Uuid id, String name, Time creation) {
    this.id = id;
    this.name = name;
    this.creation = creation;
  }
  
  public String getUUID() {
	  return id.toString();
  }
  
  public long getCreationTime() {
	  return creation.inMs();
  }


  @Override
  public boolean equals (Object toCompare)
  {
    if(!(toCompare instanceof User)) {
      return false;
    }
    User toCompareUser = (User)(toCompare);
    if(!this.id.equals(toCompareUser.id)) {
      return false;
    }
    if(!this.name.equals(toCompareUser.name)) {
      return false;
    }
    if(!this.creation.equals(toCompareUser.creation)) {
      return false;
    }
    return true;
  }

  public int hashCode() { return hash(this); }

  private static int hash(User user) {
    int hash = 0;
    hash+=user.id.hashCode();
    hash+=user.name.hashCode();
    hash+=user.creation.hashCode();
    return hash;
  }
  
  // userStatusUpdate()
  // 
  // Returns all the user updates in an dictionary 
  // where keys are the user's user interests (UUIDs) 
  // and a value is an arraylist of arraylists<String> 
  // where the 1st arraylist keeps track of each userinterest's 
  // newly created conversations (CONVOS_CREATED_ARRAY) and the second arraylist keeps 
  // track of the conversations the userinterest has contributed messages to (CONVOS_CONTRIBUTED_TO_ARRAY)
  //
  public HashMap<Uuid, ArrayList<ArrayList<String>>> userStatusUpdate(Iterable<ConversationContext> conversations, Time updateTime){ 
	  // ASSUMPTION: all of the names saved in userInterests are indeed 
	  //			 valid users in the system 
	  
	  HashMap<Uuid, ArrayList<ArrayList<String>>> userUpdates = new HashMap<Uuid, ArrayList<ArrayList<String>>>(); 


	  // creating the dictionary userUpdates
	  for (Uuid uuid : this.userInterests){
		  userUpdates.put(uuid, new ArrayList<ArrayList<String>>(Arrays.asList(
				  new ArrayList<String>() {}, new ArrayList<String>() {}))); 
	  }
	  
	  // looping through every conversation in the chat to organize updates 
	  for (ConversationContext convo : conversations){
		  MessageContext lastmessage = convo.lastMessage(); 
		  
		  if (lastmessage.message.getCreationTime() < this.lastUpdateTime){ 
			  // if the last message in the conversation was sent before lastUpdateTime 
			  // that means it was covered in the last status update and we can move on 
			  continue;
		  }
		  
		  if (convo.conversation.creation.inMs() > this.lastUpdateTime && 
				  this.userInterests.contains(convo.conversation.owner.id())){ 
			  // if the convo was created by a user of interest, it will be added 
			  // to that user's value's CONVOS_CREATED_ARRAY
			  userUpdates.get(convo.conversation.owner.id()).get(CONVOS_CREATED_ARRAY).add(convo.conversation.title); 
		  }
		  
		  
		  for (MessageContext currentmessage = convo.firstMessage(); 
				  currentmessage != null; currentmessage = currentmessage.next()){ 
			  // goes through all the messages in the conversation starting from the first one 
			  if (currentmessage.message.getCreationTime() > this.lastUpdateTime){
				  // if the currentmessage was created after the last update time 
				  // we need to look at it to see if it was contributed by a user 
				  // of interest. 
				  // If so, we add it to the user's CONVOS_CONTRIBUTED_TO_ARRAY, 
				  // otherwise we continue iterating through the messages in the convo 
				  if (this.userInterests.contains(currentmessage.message.author) && 
						  !userUpdates.get(convo.conversation.owner).get(CONVOS_CONTRIBUTED_TO_ARRAY).contains(convo.conversation.title)){
					  // Also checks to make sure that conversation hasn't already been added to CONVOS_CONTRIBUTED_TO_ARRAY
					  userUpdates.get(convo.conversation.owner).get(CONVOS_CONTRIBUTED_TO_ARRAY).add(convo.conversation.title); <commit_msg>Update User.java<commit_after>// Copyright 2017 Google Inc.
//
// Licensed 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 codeu.chat.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashMap;
import java.util.ArrayList;

import codeu.chat.client.core.ConversationContext;
import codeu.chat.client.core.MessageContext;
import codeu.chat.util.Serializer;
import codeu.chat.util.Serializers;
import codeu.chat.util.Time;
import codeu.chat.util.Uuid;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

 

// ignore anything that may cause problems when converting to JSON
// prevents crashes for items that cant be serialized.
@JsonIgnoreProperties(ignoreUnknown=true)
public final class User {
  
  public HashMap<Uuid, Integer> conversationInterests = new HashMap<>();
  public ArrayList<Uuid> userInterests = new ArrayList<Uuid>();
	
  // no need to serialize the serializer when converting to JOSN
  @JsonIgnore
  public static final Serializer<User> SERIALIZER = new Serializer<User>() {

    @Override
    public void write(OutputStream out, User value) throws IOException {

      Uuid.SERIALIZER.write(out, value.id);
      Serializers.STRING.write(out, value.name);
      Time.SERIALIZER.write(out, value.creation);

    }

    @Override
    public User read(InputStream in) throws IOException {
      return new User(
          Uuid.SERIALIZER.read(in),
          Serializers.STRING.read(in),
          Time.SERIALIZER.read(in)
      );
    }
  };

  // ignore objects that aren't built-in types (string, int, etc)
  // Jackons will default to the getters since instance vars are ignored
  @JsonIgnore
  public final Uuid id;
  public final String name;
  @JsonIgnore
  public final Time creation;
  
  // time of user's last update recorded as a long
  // initially set to user's creation time 
  public long lastUpdateTime = Time.now().inMs(); 
  public static int CONVOS_CREATED_ARRAY = 0; 
  public static int CONVOS_CONTRIBUTED_TO_ARRAY = 1; 

  public User(Uuid id, String name, Time creation) {
    this.id = id;
    this.name = name;
    this.creation = creation;
  }
  
  public String getUUID() {
	  return id.toString();
  }
  
  public long getCreationTime() {
	  return creation.inMs();
  }


  @Override
  public boolean equals (Object toCompare)
  {
    if(!(toCompare instanceof User)) {
      return false;
    }
    User toCompareUser = (User)(toCompare);
    if(!this.id.equals(toCompareUser.id)) {
      return false;
    }
    if(!this.name.equals(toCompareUser.name)) {
      return false;
    }
    if(!this.creation.equals(toCompareUser.creation)) {
      return false;
    }
    return true;
  }

  public int hashCode() { return hash(this); }

  private static int hash(User user) {
    int hash = 0;
    hash+=user.id.hashCode();
    hash+=user.name.hashCode();
    hash+=user.creation.hashCode();
    return hash;
  }
  
  // userStatusUpdate()
  // 
  // Returns all the user updates in an dictionary 
  // where keys are the user's user interests (UUIDs) 
  // and a value is an arraylist of arraylists<String> 
  // where the 1st arraylist keeps track of each userinterest's 
  // newly created conversations (CONVOS_CREATED_ARRAY) and the second arraylist keeps 
  // track of the conversations the userinterest has contributed messages to (CONVOS_CONTRIBUTED_TO_ARRAY)
  //
  public HashMap<Uuid, ArrayList<ArrayList<String>>> userStatusUpdate(Iterable<ConversationContext> conversations, Time updateTime){ 
	  // ASSUMPTION: all of the names saved in userInterests are indeed 
	  //			 valid users in the system 
	  
	  HashMap<Uuid, ArrayList<ArrayList<String>>> userUpdates = new HashMap<Uuid, ArrayList<ArrayList<String>>>(); 


	  // creating the dictionary userUpdates
	  for (Uuid uuid : this.userInterests){
		  userUpdates.put(uuid, new ArrayList<ArrayList<String>>(Arrays.asList(
				  new ArrayList<String>() {}, new ArrayList<String>() {}))); 
	  }
	  
	  // looping through every conversation in the chat to organize updates 
	  for (ConversationContext convo : conversations){
		  MessageContext lastmessage = convo.lastMessage(); 
		  
		  if (lastmessage.message.getCreationTime() < this.lastUpdateTime){ 
			  // if the last message in the conversation was sent before lastUpdateTime 
			  // that means it was covered in the last status update and we can move on 
			  continue;
		  }
		  
		  if (convo.conversation.creation.inMs() > this.lastUpdateTime && 
				  this.userInterests.contains(convo.conversation.owner)){ 
			  // if the convo was created by a user of interest, it will be added 
			  // to that user's value's CONVOS_CREATED_ARRAY
			  userUpdates.get(convo.conversation.owner).get(CONVOS_CREATED_ARRAY).add(convo.conversation.title); 
		  }
		  
		  
		  for (MessageContext currentmessage = convo.firstMessage(); 
				  currentmessage != null; currentmessage = currentmessage.next()){ 
			  // goes through all the messages in the conversation starting from the first one 
			  if (currentmessage.message.getCreationTime() > this.lastUpdateTime){
				  // if the currentmessage was created after the last update time 
				  // we need to look at it to see if it was contributed by a user 
				  // of interest. 
				  // If so, we add it to the user's CONVOS_CONTRIBUTED_TO_ARRAY, 
				  // otherwise we continue iterating through the messages in the convo 
				  if (this.userInterests.contains(currentmessage.message.author) && 
						  !userUpdates.get(convo.conversation.owner).get(CONVOS_CONTRIBUTED_TO_ARRAY).contains(convo.conversation.title)){
					  // Also checks to make sure that conversation hasn't already been added to CONVOS_CONTRIBUTED_TO_ARRAY
					  userUpdates.get(convo.conversation.owner).get(CONVOS_CONTRIBUTED_TO_ARRAY).add(convo.conversation.title); 
{'fraction_non_alphanumeric': 0.09445346948247853, 'fraction_numerical': 0.0018565792527268509, 'mean_word_length': 3.444138879339979, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '795644', 'n_tokens_mistral': 4002, 'n_tokens_neox': 3651, 'n_words': 1163}