/* * Artist - represents one artist */ class Artist{ int maxLookBack=11; // Max we can round String name; // Name of this artist Week firstPlay; // Pointer to the first week this artist was listened to Week lastPlay; // Pointer to the last week this artist was listened to int totalPlays; // The total play count for this artist Vector weeklyPlays; // The list of plays per week, identical in order to Weeks Artists parent; // The artists collection that contains this artist float popularity; // The popularity of this artist float maxPopularity; // The max popularity this artist has been float popularityCache[][]; //weeks, lookbehind int id; // This is the ID of the artist that never changes, used as unique id int index; // This is the index of this artist so that the map can find it's position in the list Artist(String name, int index, Artists parent){ this.name = name; this.id = parent.artistList.size(); this.index = index; this.parent = parent; firstPlay = null; lastPlay = null; totalPlays = 0; popularity = -1; maxPopularity = -1; weeklyPlays = new Vector(); } String getName(){ return name; } int getID(){ return id; } Week getFirstPlay(){ return firstPlay; } int getTotalPlays(){ return totalPlays; } float getPopularity(){ if(popularity != -1) return popularity; if(totalPlays == 1 && parent.maxPlays == 1) return 1; if(totalPlays <= 1 || parent.maxPlays <= 1) return 0; popularity = log(totalPlays) / log(parent.maxPlays); return popularity; } float getMaxPopularity(){ if(maxPopularity != -1) return maxPopularity; int totalWeeks = parent.parent.weeks.getCount(); maxPopularity = 0; for(int i=0; i maxLookBack || lookBack < 0) lookBack = constrain(maxLookBack,0,maxLookBack); int id = w.getID(); // constrain if(lookAhead>0){ int newWeekID = min(id + lookAhead, parent.parent.weeks.getCount()-1); int newLookBack = lookBack + (newWeekID - id); return getPopularity(newWeekID, newLookBack, 0); } // do cache if(popularityCache==null){ clearPopularityCache(); }else if(popularityCache[id][lookBack]!=-1){ return popularityCache[id][lookBack]; } int totalPlays=0; int artistPlays=0; int lookingBack=0; do{ totalPlays += w.totalPlays; artistPlays += this.getPlays(w); w = w.getPrev(); }while(w != null && lookingBack++= weeklyPlays.size()) return 0; Integer weekPlays = (Integer) weeklyPlays.get(id); if(weekPlays == null) return 0; return weekPlays.intValue(); } void addPlays(Week w, int plays){ // add plays to me totalPlays += plays; // check max against artists parent.maxPlays = max(parent.maxPlays, totalPlays); // assess week lastPlay = w; if(firstPlay == null) firstPlay = w; // add plays to artist/week int id = w.getID(); if(id >= weeklyPlays.size()) weeklyPlays.setSize(id+1); weeklyPlays.set(id, new Integer(plays) ); // add plays to week w.totalPlays += plays; // check max against week / weeks w.maxPlays = max(w.maxPlays, plays); w.parent.maxPlays = max(w.parent.maxPlays, w.totalPlays); // add plays to total parent.parent.totalPlays += plays; } }