Example usage for javax.json JsonArray size

List of usage examples for javax.json JsonArray size

Introduction

In this page you can find the example usage for javax.json JsonArray size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this list.

Usage

From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java

private void importTalkTypeList() {
    this.talkTypeNameToTalkTypeMap = new HashMap<>();
    List<TalkType> talkTypeList = new ArrayList<>();

    String proposalTypeUrl = conferenceBaseUrl + "/proposalTypes";
    LOGGER.debug("Sending a request to: " + proposalTypeUrl);
    JsonObject rootObject = readJson(proposalTypeUrl, JsonReader::readObject);

    JsonArray talkTypeArray = rootObject.getJsonArray("proposalTypes");
    for (int i = 0; i < talkTypeArray.size(); i++) {
        JsonObject talkTypeObject = talkTypeArray.getJsonObject(i);
        String talkTypeName = talkTypeObject.getString("id");
        if (talkTypeNameToTalkTypeMap.keySet().contains(talkTypeName)) {
            LOGGER.warn("Duplicate talk type in " + proposalTypeUrl + " at index " + i + ".");
            continue;
        }// w  ww  .java2 s.c o m

        TalkType talkType = new TalkType((long) i, talkTypeName);
        talkType.setCompatibleRoomSet(new HashSet<>());
        talkType.setCompatibleTimeslotSet(new HashSet<>());

        talkTypeList.add(talkType);
        talkTypeNameToTalkTypeMap.put(talkTypeName, talkType);
    }

    solution.setTalkTypeList(talkTypeList);
}

From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java

private void importTrackIdSet() {
    this.trackIdSet = new HashSet<>();
    String tracksUrl = conferenceBaseUrl + "/tracks";
    LOGGER.debug("Sending a request to: " + tracksUrl);
    JsonObject rootObject = readJson(tracksUrl, JsonReader::readObject);

    JsonArray tracksArray = rootObject.getJsonArray("tracks");
    for (int i = 0; i < tracksArray.size(); i++) {
        trackIdSet.add(tracksArray.getJsonObject(i).getString("id"));
    }/*from ww w . j av  a 2s. co m*/
}

From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java

private void importRoomList() {
    this.roomIdToRoomMap = new HashMap<>();
    List<Room> roomList = new ArrayList<>();

    String roomsUrl = conferenceBaseUrl + "/rooms/";
    LOGGER.debug("Sending a request to: " + roomsUrl);
    JsonObject rootObject = readJson(roomsUrl, JsonReader::readObject);

    JsonArray roomArray = rootObject.getJsonArray("rooms");
    for (int i = 0; i < roomArray.size(); i++) {
        JsonObject roomObject = roomArray.getJsonObject(i);
        String id = roomObject.getString("id");
        int capacity = roomObject.getInt("capacity");

        if (!Arrays.asList(IGNORED_ROOM_IDS).contains(id)) {
            Room room = new Room((long) i);
            room.setName(id);//  ww w .j av  a 2s . c om
            room.setCapacity(capacity);
            room.setTalkTypeSet(getTalkTypeSetForCapacity(capacity));
            for (TalkType talkType : room.getTalkTypeSet()) {
                talkType.getCompatibleRoomSet().add(room);
            }
            room.setTagSet(new HashSet<>());
            room.setUnavailableTimeslotSet(new HashSet<>());
            roomList.add(room);
            roomIdToRoomMap.put(id, room);
        }
    }

    roomList.sort(Comparator.comparing(Room::getName));
    solution.setRoomList(roomList);
}

From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java

private void importSpeakerList() {
    this.speakerNameToSpeakerMap = new HashMap<>();
    this.talkUrlSet = new HashSet<>();
    List<Speaker> speakerList = new ArrayList<>();

    String speakersUrl = conferenceBaseUrl + "/speakers";
    LOGGER.debug("Sending a request to: " + speakersUrl);
    JsonArray speakerArray = readJson(speakersUrl, JsonReader::readArray);

    for (int i = 0; i < speakerArray.size(); i++) {
        String speakerUrl = speakerArray.getJsonObject(i).getJsonArray("links").getJsonObject(0)
                .getString("href");
        LOGGER.debug("Sending a request to: " + speakerUrl);
        JsonObject speakerObject = readJson(speakerUrl, JsonReader::readObject);

        String speakerId = speakerObject.getString("uuid");
        String speakerName = speakerObject.getString("firstName") + " " + speakerObject.getString("lastName");

        if (Arrays.asList(IGNORED_SPEAKER_NAMES).contains(speakerName)) {
            continue;
        }/*from  w w  w.ja va 2 s.c  o  m*/
        Speaker speaker = new Speaker((long) i);
        speaker.setName(speakerName);
        speaker.withPreferredRoomTagSet(new HashSet<>()).withPreferredTimeslotTagSet(new HashSet<>())
                .withProhibitedRoomTagSet(new HashSet<>()).withProhibitedTimeslotTagSet(new HashSet<>())
                .withRequiredRoomTagSet(new HashSet<>()).withRequiredTimeslotTagSet(new HashSet<>())
                .withUnavailableTimeslotSet(new HashSet<>()).withUndesiredRoomTagSet(new HashSet<>())
                .withUndesiredTimeslotTagSet(new HashSet<>());
        speakerList.add(speaker);
        if (speakerNameToSpeakerMap.keySet().contains(speakerName)) {
            throw new IllegalStateException("Speaker (" + speakerName + ") with id (" + speakerId
                    + ") already exists in the speaker list");
        }
        speakerNameToSpeakerMap.put(speakerName, speaker);

        JsonArray speakerTalksArray = speakerObject.getJsonArray("acceptedTalks");
        for (int j = 0; j < speakerTalksArray.size(); j++) {
            String talkUrl = speakerTalksArray.getJsonObject(j).getJsonArray("links").getJsonObject(0)
                    .getString("href");
            talkUrlSet.add(talkUrl);
        }
    }

    speakerList.sort(Comparator.comparing(Speaker::getName));
    solution.setSpeakerList(speakerList);
}

From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java

private void importTalkList() {
    this.talkCodeToTalkMap = new HashMap<>();
    List<Talk> talkList = new ArrayList<>();
    Long talkId = 0L;/*from   w ww .j av a  2 s .  c  o  m*/

    String talksPath = getClass().getResource("devoxxBE").toString();
    String[] confFiles = { "BOF", "Conf14Sept2018", "DeepDive", "HandsOnLabs", "Quickies", "ToolsInAction" };
    for (String confType : confFiles) {
        LOGGER.debug("Sending a request to: " + talksPath + "/" + confType + ".json");
        JsonArray talksArray = readJson(talksPath + "/" + confType + ".json", JsonReader::readObject)
                .getJsonObject("approvedTalks").getJsonArray("talks");

        for (int i = 0; i < talksArray.size(); i++) {
            JsonObject talkObject = talksArray.getJsonObject(i);

            String code = talkObject.getString("id");
            String title = talkObject.getString("title").substring(5);
            String talkTypeName = talkObject.getJsonObject("talkType").getString("id");
            Set<String> themeTrackSet = extractThemeTrackSet(talkObject, code, title);
            String language = talkObject.getString("lang");
            int audienceLevel = Integer
                    .parseInt(talkObject.getString("audienceLevel").replaceAll("[^0-9]", ""));
            List<Speaker> speakerList = extractSpeakerList(confType, talkObject, code, title);
            Set<String> contentTagSet = extractContentTagSet(talkObject);
            String state = talkObject.getJsonObject("state").getString("code");

            if (!Arrays.asList(IGNORED_TALK_TYPES).contains(code) && !state.equals("declined")) {
                Talk talk = createTalk(talkId++, code, title, talkTypeName, themeTrackSet, language,
                        speakerList, audienceLevel, contentTagSet);
                talkCodeToTalkMap.put(code, talk);
                talkList.add(talk);
                talkTalkTypeToTotalMap.merge(talkTypeName, 1, Integer::sum);
            }
        }
    }
    solution.setTalkList(talkList);
}

From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java

private void importTimeslotList() {
    List<Timeslot> timeslotList = new ArrayList<>();
    Map<Timeslot, List<Room>> timeslotToAvailableRoomsMap = new HashMap<>();
    Map<Pair<LocalDateTime, LocalDateTime>, Timeslot> startAndEndTimeToTimeslotMap = new HashMap<>();

    Long timeSlotId = 0L;//  w ww .  j a  va2 s. c  o  m
    String schedulesUrl = conferenceBaseUrl + "/schedules/";
    LOGGER.debug("Sending a request to: " + schedulesUrl);
    JsonArray daysArray = readJson(schedulesUrl, JsonReader::readObject).getJsonArray("links");
    for (int i = 0; i < daysArray.size(); i++) {
        JsonObject dayObject = daysArray.getJsonObject(i);
        String dayUrl = dayObject.getString("href");

        LOGGER.debug("Sending a request to: " + dayUrl);
        JsonArray daySlotsArray = readJson(dayUrl, JsonReader::readObject).getJsonArray("slots");

        for (int j = 0; j < daySlotsArray.size(); j++) {
            JsonObject timeslotObject = daySlotsArray.getJsonObject(j);

            LocalDateTime startDateTime = LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(timeslotObject.getJsonNumber("fromTimeMillis").longValue()),
                    ZoneId.of(ZONE_ID));
            LocalDateTime endDateTime = LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(timeslotObject.getJsonNumber("toTimeMillis").longValue()),
                    ZoneId.of(ZONE_ID));

            Room room = roomIdToRoomMap.get(timeslotObject.getString("roomId"));
            if (room == null) {
                throw new IllegalStateException("The timeslot (" + timeslotObject.getString("slotId")
                        + ") has a roomId (" + timeslotObject.getString("roomId")
                        + ") that does not exist in the rooms list");
            }

            // Assuming slotId is of format: tia_room6_monday_12_.... take only "tia"
            String talkTypeName = timeslotObject.getString("slotId").split("_")[0];
            TalkType timeslotTalkType = talkTypeNameToTalkTypeMap.get(talkTypeName);
            if (Arrays.asList(IGNORED_TALK_TYPES).contains(talkTypeName)) {
                continue;
            }

            Timeslot timeslot;
            if (startAndEndTimeToTimeslotMap.keySet().contains(Pair.of(startDateTime, endDateTime))) {
                timeslot = startAndEndTimeToTimeslotMap.get(Pair.of(startDateTime, endDateTime));
                timeslotToAvailableRoomsMap.get(timeslot).add(room);
                if (timeslotTalkType != null) {
                    timeslot.getTalkTypeSet().add(timeslotTalkType);
                }
            } else {
                timeslot = new Timeslot(timeSlotId++);
                timeslot.withStartDateTime(startDateTime).withEndDateTime(endDateTime)
                        .withTalkTypeSet(timeslotTalkType == null ? new HashSet<>()
                                : new HashSet<>(Arrays.asList(timeslotTalkType)));
                timeslot.setTagSet(new HashSet<>());

                timeslotList.add(timeslot);
                timeslotToAvailableRoomsMap.put(timeslot, new ArrayList<>(Arrays.asList(room)));
                startAndEndTimeToTimeslotMap.put(Pair.of(startDateTime, endDateTime), timeslot);
            }

            if (!timeslotObject.isNull("talk")) {
                scheduleTalk(timeslotObject, room, timeslot);
            }

            for (TalkType talkType : timeslot.getTalkTypeSet()) {
                talkType.getCompatibleTimeslotSet().add(timeslot);
            }
            timeslotTalkTypeToTotalMap.merge(talkTypeName, 1, Integer::sum);
        }
    }

    for (Room room : solution.getRoomList()) {
        room.setUnavailableTimeslotSet(timeslotList.stream()
                .filter(timeslot -> !timeslotToAvailableRoomsMap.get(timeslot).contains(room))
                .collect(Collectors.toSet()));
    }

    solution.setTimeslotList(timeslotList);
}